klib 1.4.0__tar.gz → 1.4.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. {klib-1.4.0 → klib-1.4.1}/.github/workflows/codeql-analysis.yml +1 -1
  2. {klib-1.4.0 → klib-1.4.1}/.github/workflows/pre-commit.yaml +2 -2
  3. {klib-1.4.0 → klib-1.4.1}/.github/workflows/pythonpackage.yml +8 -5
  4. {klib-1.4.0 → klib-1.4.1}/.pre-commit-config.yaml +1 -1
  5. {klib-1.4.0 → klib-1.4.1}/PKG-INFO +6 -1
  6. {klib-1.4.0 → klib-1.4.1}/pyproject.toml +9 -5
  7. {klib-1.4.0 → klib-1.4.1}/src/klib/_version.py +1 -1
  8. {klib-1.4.0 → klib-1.4.1}/src/klib/clean.py +9 -6
  9. {klib-1.4.0 → klib-1.4.1}/src/klib/describe.py +61 -36
  10. {klib-1.4.0 → klib-1.4.1}/src/klib/utils.py +1 -1
  11. klib-1.4.1/tests/conftest.py +5 -0
  12. {klib-1.4.0 → klib-1.4.1}/tests/test_clean.py +21 -23
  13. {klib-1.4.0 → klib-1.4.1}/tests/test_describe.py +79 -0
  14. klib-1.4.1/uv.lock +2907 -0
  15. klib-1.4.0/uv.lock +0 -2546
  16. {klib-1.4.0 → klib-1.4.1}/.github/FUNDING.yml +0 -0
  17. {klib-1.4.0 → klib-1.4.1}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
  18. {klib-1.4.0 → klib-1.4.1}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
  19. {klib-1.4.0 → klib-1.4.1}/.github/dependabot.yml +0 -0
  20. {klib-1.4.0 → klib-1.4.1}/.gitignore +0 -0
  21. {klib-1.4.0 → klib-1.4.1}/CODE_OF_CONDUCT.md +0 -0
  22. {klib-1.4.0 → klib-1.4.1}/LICENSE +0 -0
  23. {klib-1.4.0 → klib-1.4.1}/MANIFEST.in +0 -0
  24. {klib-1.4.0 → klib-1.4.1}/Makefile +0 -0
  25. {klib-1.4.0 → klib-1.4.1}/README.md +0 -0
  26. {klib-1.4.0 → klib-1.4.1}/examples/NFL_DATASET.csv +0 -0
  27. {klib-1.4.0 → klib-1.4.1}/examples/README.md +0 -0
  28. {klib-1.4.0 → klib-1.4.1}/examples/datasets/.gitignore +0 -0
  29. {klib-1.4.0 → klib-1.4.1}/examples/images/example_cat_plot.png +0 -0
  30. {klib-1.4.0 → klib-1.4.1}/examples/images/example_corr_interactive_plot.png +0 -0
  31. {klib-1.4.0 → klib-1.4.1}/examples/images/example_corr_plot.png +0 -0
  32. {klib-1.4.0 → klib-1.4.1}/examples/images/example_dist_plot.png +0 -0
  33. {klib-1.4.0 → klib-1.4.1}/examples/images/example_klib_corr_mat.png +0 -0
  34. {klib-1.4.0 → klib-1.4.1}/examples/images/example_klib_data_cleaning_dtypes.png +0 -0
  35. {klib-1.4.0 → klib-1.4.1}/examples/images/example_klib_data_cleaning_report.png +0 -0
  36. {klib-1.4.0 → klib-1.4.1}/examples/images/example_klib_pool_duplicate_subsets1.png +0 -0
  37. {klib-1.4.0 → klib-1.4.1}/examples/images/example_klib_pool_duplicate_subsets2.png +0 -0
  38. {klib-1.4.0 → klib-1.4.1}/examples/images/example_klib_pool_duplicate_subsets3.png +0 -0
  39. {klib-1.4.0 → klib-1.4.1}/examples/images/example_mv_plot.png +0 -0
  40. {klib-1.4.0 → klib-1.4.1}/examples/images/example_target_corr_plot.png +0 -0
  41. {klib-1.4.0 → klib-1.4.1}/examples/images/header.png +0 -0
  42. {klib-1.4.0 → klib-1.4.1}/examples/klib-plots.ipynb +0 -0
  43. {klib-1.4.0 → klib-1.4.1}/examples/klib_data_cleaning.ipynb +0 -0
  44. {klib-1.4.0 → klib-1.4.1}/src/klib/__init__.py +0 -0
  45. {klib-1.4.0 → klib-1.4.1}/src/klib/scripts/__init__.py +0 -0
  46. {klib-1.4.0 → klib-1.4.1}/src/klib/scripts/performance.py +0 -0
  47. {klib-1.4.0 → klib-1.4.1}/tests/__init__.py +0 -0
  48. {klib-1.4.0 → klib-1.4.1}/tests/test_util.py +0 -0
@@ -16,7 +16,7 @@ jobs:
16
16
 
17
17
  steps:
18
18
  - name: Checkout repository
19
- uses: actions/checkout@v5
19
+ uses: actions/checkout@v6
20
20
  with:
21
21
  # We must fetch at least the immediate parents so that if this is
22
22
  # a pull request then we can checkout the head.
@@ -11,9 +11,9 @@ jobs:
11
11
  runs-on: ubuntu-latest
12
12
  if: ${{ github.event.pull_request.draft == false }}
13
13
  steps:
14
- - uses: actions/checkout@v5
14
+ - uses: actions/checkout@v6
15
15
  - name: Setup python
16
- uses: actions/setup-python@v5
16
+ uses: actions/setup-python@v6
17
17
  with:
18
18
  python-version: "3.12"
19
19
  - uses: pre-commit/action@v3.0.1
@@ -12,14 +12,14 @@ jobs:
12
12
  build:
13
13
  strategy:
14
14
  matrix:
15
- python-version: ["3.10", "3.11", "3.12", "3.13"]
15
+ python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
16
16
  platform: [ubuntu-latest, macos-latest, windows-latest]
17
17
 
18
18
  runs-on: ${{ matrix.platform }}
19
19
  steps:
20
- - uses: actions/checkout@v5
20
+ - uses: actions/checkout@v6
21
21
  - name: Set up Python ${{ matrix.python-version }}
22
- uses: actions/setup-python@v5
22
+ uses: actions/setup-python@v6
23
23
  with:
24
24
  python-version: ${{ matrix.python-version }}
25
25
 
@@ -38,7 +38,10 @@ jobs:
38
38
 
39
39
  - name: Install dependencies (Unix)
40
40
  if: runner.os != 'Windows'
41
- run: make create-venv
41
+ run: |
42
+ uv venv --python ${{ matrix.python-version }}
43
+ . .venv/bin/activate
44
+ uv sync --frozen --all-extras
42
45
 
43
46
  - name: Test with pytest and generate coverage report
44
47
  run: |
@@ -63,7 +66,7 @@ jobs:
63
66
  version: "latest"
64
67
 
65
68
  - name: Set up Python
66
- uses: actions/setup-python@v5
69
+ uses: actions/setup-python@v6
67
70
  with:
68
71
  python-version: "3.12"
69
72
 
@@ -26,7 +26,7 @@ repos:
26
26
  - id: name-tests-test
27
27
  args: ["--pytest-test-first"]
28
28
  - repo: https://github.com/astral-sh/ruff-pre-commit
29
- rev: v0.9.2
29
+ rev: v0.15.17
30
30
  hooks:
31
31
  - id: ruff
32
32
  args: [--fix, --exit-non-zero-on-fix, --show-fixes]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: klib
3
- Version: 1.4.0
3
+ Version: 1.4.1
4
4
  Summary: Common data preprocessing and visualisation functions.
5
5
  Author-email: Andreas Kanz <andreas@akanz.de>
6
6
  License-Expression: MIT
@@ -8,6 +8,11 @@ License-File: LICENSE
8
8
  Classifier: License :: OSI Approved :: MIT License
9
9
  Classifier: Operating System :: OS Independent
10
10
  Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: Programming Language :: Python :: 3.14
11
16
  Requires-Python: >=3.10
12
17
  Requires-Dist: jinja2>=3.1.0
13
18
  Requires-Dist: matplotlib>=3.9.0
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "klib"
3
- version = "1.4.0"
3
+ version = "1.4.1"
4
4
  description = "Common data preprocessing and visualisation functions."
5
5
  authors = [{ name = "Andreas Kanz", email = "andreas@akanz.de" }]
6
6
  license = "MIT"
@@ -8,6 +8,11 @@ readme = "README.md"
8
8
  requires-python = ">=3.10"
9
9
  classifiers = [
10
10
  "Programming Language :: Python :: 3",
11
+ "Programming Language :: Python :: 3.10",
12
+ "Programming Language :: Python :: 3.11",
13
+ "Programming Language :: Python :: 3.12",
14
+ "Programming Language :: Python :: 3.13",
15
+ "Programming Language :: Python :: 3.14",
11
16
  "License :: OSI Approved :: MIT License",
12
17
  "Operating System :: OS Independent",
13
18
  ]
@@ -65,7 +70,7 @@ branch = true
65
70
  data_file = "tests/.coverage"
66
71
 
67
72
  [tool.coverage.report]
68
- fail_skip_covered = true
73
+ skip_covered = true
69
74
  exclude_also = [
70
75
  "if self.debug:",
71
76
  "if settings.DEBUG",
@@ -82,7 +87,7 @@ exclude_also = [
82
87
 
83
88
  [tool.ruff]
84
89
  line-length = 100
85
- target-version = "py313"
90
+ target-version = "py314"
86
91
  fix = true
87
92
 
88
93
  lint.select = ["ALL"]
@@ -108,8 +113,7 @@ lint.ignore = [
108
113
  "PLR0913",
109
114
  "D213",
110
115
  "D203",
111
- "UP038",
112
- "PD901",
116
+ "RUF043",
113
117
  ]
114
118
 
115
119
  extend-exclude = ["examples"]
@@ -1,3 +1,3 @@
1
1
  """Current version of klib."""
2
2
 
3
- __version__ = "1.4.0"
3
+ __version__ = "1.4.1"
@@ -107,13 +107,11 @@ def clean_column_names(data: pd.DataFrame, *, hints: bool = True) -> pd.DataFram
107
107
  col if col not in data.columns[:i] else f"{col}_{i!s}"
108
108
  for i, col in enumerate(data.columns)
109
109
  ]
110
- print_statement = ""
111
110
  if hints:
112
- # add string to print statement
113
111
  print_statement = (
114
112
  f"Duplicate column names detected! Columns with index {dupl_idx} and "
115
113
  f"names {dupl_before} have been renamed to "
116
- f"{data.columns[dupl_idx].tolist()}.",
114
+ f"{data.columns[dupl_idx].tolist()}."
117
115
  )
118
116
 
119
117
  if long_col_names := [
@@ -122,8 +120,8 @@ def clean_column_names(data: pd.DataFrame, *, hints: bool = True) -> pd.DataFram
122
120
  if len(x) > 25 # noqa: PLR2004
123
121
  ]:
124
122
  print_statement += (
125
- "Long column names detected (>25 characters). Consider renaming "
126
- f"the following columns {long_col_names}.",
123
+ " Long column names detected (>25 characters). Consider renaming "
124
+ f"the following columns {long_col_names}."
127
125
  )
128
126
 
129
127
  print(print_statement)
@@ -371,6 +369,11 @@ def data_cleaning(
371
369
  cat_threshold=cat_threshold,
372
370
  cat_exclude=cat_exclude,
373
371
  )
372
+ else:
373
+ string_cols = [
374
+ col for col in data_cleaned.columns if data_cleaned[col].dtype.name in ("str", "string")
375
+ ]
376
+ data_cleaned[string_cols] = data_cleaned[string_cols].astype("object")
374
377
 
375
378
  _diff_report(
376
379
  data,
@@ -450,7 +453,7 @@ def mv_col_handling(
450
453
  data_local = data.copy()
451
454
  mv_ratios = _missing_vals(data_local)["mv_cols_ratio"]
452
455
  cols_mv = mv_ratios[mv_ratios > mv_threshold].index.tolist()
453
- data_local[cols_mv] = data_local[cols_mv].map(lambda x: x if pd.isna(x) else 1).fillna(0)
456
+ data_local[cols_mv] = data_local[cols_mv].notna().astype(int)
454
457
 
455
458
  high_corr_features = []
456
459
  data_temp = data_local.copy()
@@ -89,7 +89,7 @@ def cat_plot( # noqa: C901, PLR0915
89
89
  return None
90
90
 
91
91
  for col in data.columns:
92
- if data[col].dtype.name in ("category", "string"):
92
+ if data[col].dtype.name in ("category", "str", "string"):
93
93
  data[col] = data[col].astype("object")
94
94
 
95
95
  fig = plt.figure(figsize=figsize)
@@ -272,7 +272,13 @@ def corr_mat(
272
272
  corr = _corr_selector(corr, split=split, threshold=threshold)
273
273
 
274
274
  if colored:
275
- return corr.style.map(color_negative_red).format("{:.2f}", na_rep="-")
275
+ styler = corr.style
276
+ styler = (
277
+ styler.map(color_negative_red)
278
+ if hasattr(styler, "map")
279
+ else styler.applymap(color_negative_red)
280
+ )
281
+ return styler.format("{:.2f}", na_rep="-")
276
282
  return corr
277
283
 
278
284
 
@@ -287,6 +293,7 @@ def corr_plot(
287
293
  *,
288
294
  annot: bool = True,
289
295
  dev: bool = False,
296
+ ax: plt.Axes | None = None,
290
297
  **kwargs, # noqa: ANN003
291
298
  ) -> plt.Axes:
292
299
  """2D visualization of the correlation between feature-columns excluding NA values.
@@ -334,6 +341,8 @@ def corr_plot(
334
341
  dev : bool, optional
335
342
  Display figure settings in the plot by setting dev = True. If False, the \
336
343
  settings are not displayed, by default False
344
+ ax : matplotlib Axes, optional
345
+ Existing Axes object to draw into, by default None.
337
346
 
338
347
  kwargs : optional
339
348
  Additional elements to control the visualization of the plot, e.g.:
@@ -389,7 +398,10 @@ def corr_plot(
389
398
  vmax = np.round(np.nanmax(corr.where(~mask)) - 0.05, 2)
390
399
  vmin = np.round(np.nanmin(corr.where(~mask)) + 0.05, 2)
391
400
 
392
- fig, ax = plt.subplots(figsize=figsize)
401
+ if ax is None:
402
+ fig, ax = plt.subplots(figsize=figsize)
403
+ else:
404
+ fig = ax.figure
393
405
 
394
406
  # Specify kwargs for the heatmap
395
407
  kwargs = {
@@ -401,6 +413,7 @@ def corr_plot(
401
413
  "linewidths": 0.5,
402
414
  "annot_kws": {"size": 10},
403
415
  "cbar_kws": {"shrink": 0.95, "aspect": 30},
416
+ "ax": ax,
404
417
  **kwargs,
405
418
  }
406
419
 
@@ -586,7 +599,7 @@ def corr_interactive_plot( # noqa: C901
586
599
 
587
600
  if target is None:
588
601
  mask = np.triu(np.ones_like(corr, dtype=bool))
589
- np.fill_diagonal(corr.to_numpy(), np.nan)
602
+ corr = corr.mask(np.eye(len(corr), dtype=bool))
590
603
  corr = corr.where(mask == 1)
591
604
  else:
592
605
  corr = corr.iloc[::-1, :]
@@ -663,7 +676,7 @@ def corr_interactive_plot( # noqa: C901
663
676
  return heatmap
664
677
 
665
678
 
666
- def dist_plot(
679
+ def dist_plot( # noqa: PLR0915
667
680
  data: pd.DataFrame,
668
681
  mean_color: str = "orange",
669
682
  size: int = 3,
@@ -673,6 +686,7 @@ def dist_plot(
673
686
  rug_kws: dict[str, Any] | None = None,
674
687
  fill_kws: dict[str, Any] | None = None,
675
688
  font_kws: dict[str, Any] | None = None,
689
+ ax: plt.Axes | None = None,
676
690
  ) -> None | Any: # noqa: ANN401
677
691
  """2D visualization of the distribution of non binary numerical features.
678
692
 
@@ -702,6 +716,8 @@ def dist_plot(
702
716
  font_kws : dict[str, Any], optional
703
717
  Keyword arguments to control the font, by default {"color": "#111111", \
704
718
  "weight": "normal", "size": 11}
719
+ ax : matplotlib Axes, optional
720
+ Existing Axes object to draw a single numeric column into, by default None.
705
721
 
706
722
  Returns
707
723
  -------
@@ -757,25 +773,34 @@ def dist_plot(
757
773
  if not cols:
758
774
  print("No columns with numeric data were detected.")
759
775
  return None
776
+ if ax is not None and len(cols) != 1:
777
+ msg = "When ax is provided, dist_plot requires exactly one numeric non-binary column."
778
+ raise ValueError(msg)
760
779
 
761
780
  for col in cols:
762
781
  col_data = data[col].dropna(axis=0)
763
782
  col_df = df[col].dropna(axis=0)
764
783
 
765
- g = sns.displot(
766
- col_data,
767
- kind="kde",
768
- rug=True,
769
- height=size,
770
- aspect=5,
771
- legend=False,
772
- rug_kws=rug_kws,
773
- **kde_kws,
774
- )
784
+ if ax is None:
785
+ g = sns.displot(
786
+ col_data,
787
+ kind="kde",
788
+ rug=True,
789
+ height=size,
790
+ aspect=5,
791
+ legend=False,
792
+ rug_kws=rug_kws,
793
+ **kde_kws,
794
+ )
795
+ plot_ax = g.axes[0, 0]
796
+ else:
797
+ sns.kdeplot(col_data, ax=ax, **kde_kws)
798
+ sns.rugplot(col_data, ax=ax, **rug_kws)
799
+ plot_ax = ax
775
800
 
776
801
  # Vertical lines and fill
777
- x, y = g.axes[0, 0].lines[0].get_xydata().T
778
- g.axes[0, 0].fill_between(
802
+ x, y = plot_ax.lines[0].get_xydata().T
803
+ plot_ax.fill_between(
779
804
  x,
780
805
  y,
781
806
  where=(
@@ -788,7 +813,7 @@ def dist_plot(
788
813
 
789
814
  mean = np.mean(col_df)
790
815
  std = scipy.stats.tstd(col_df)
791
- g.axes[0, 0].vlines(
816
+ plot_ax.vlines(
792
817
  x=mean,
793
818
  ymin=0,
794
819
  ymax=np.interp(mean, x, y),
@@ -797,7 +822,7 @@ def dist_plot(
797
822
  lw=2,
798
823
  label="mean",
799
824
  )
800
- g.axes[0, 0].vlines(
825
+ plot_ax.vlines(
801
826
  x=np.median(col_df),
802
827
  ymin=0,
803
828
  ymax=np.interp(np.median(col_df), x, y),
@@ -805,7 +830,7 @@ def dist_plot(
805
830
  color=".3",
806
831
  label="median",
807
832
  )
808
- g.axes[0, 0].vlines(
833
+ plot_ax.vlines(
809
834
  x=[mean - std, mean + std],
810
835
  ymin=0,
811
836
  ymax=[np.interp(mean - std, x, y), np.interp(mean + std, x, y)],
@@ -814,51 +839,51 @@ def dist_plot(
814
839
  label="\u03bc \u00b1 \u03c3",
815
840
  )
816
841
 
817
- g.axes[0, 0].set_ylim(0)
818
- g.axes[0, 0].set_xlim(
819
- g.axes[0, 0].get_xlim()[0] - g.axes[0, 0].get_xlim()[1] * 0.05,
820
- g.axes[0, 0].get_xlim()[1] * 1.03,
842
+ plot_ax.set_ylim(0)
843
+ plot_ax.set_xlim(
844
+ plot_ax.get_xlim()[0] - plot_ax.get_xlim()[1] * 0.05,
845
+ plot_ax.get_xlim()[1] * 1.03,
821
846
  )
822
847
 
823
848
  # Annotations and legend
824
- g.axes[0, 0].text(
849
+ plot_ax.text(
825
850
  0.005,
826
851
  0.9,
827
852
  f"Mean: {mean:.2f}",
828
853
  fontdict=font_kws,
829
- transform=g.axes[0, 0].transAxes,
854
+ transform=plot_ax.transAxes,
830
855
  )
831
- g.axes[0, 0].text(
856
+ plot_ax.text(
832
857
  0.005,
833
858
  0.7,
834
859
  f"Std. dev: {std:.2f}",
835
860
  fontdict=font_kws,
836
- transform=g.axes[0, 0].transAxes,
861
+ transform=plot_ax.transAxes,
837
862
  )
838
- g.axes[0, 0].text(
863
+ plot_ax.text(
839
864
  0.005,
840
865
  0.5,
841
866
  f"Skew: {scipy.stats.skew(col_df):.2f}",
842
867
  fontdict=font_kws,
843
- transform=g.axes[0, 0].transAxes,
868
+ transform=plot_ax.transAxes,
844
869
  )
845
- g.axes[0, 0].text(
870
+ plot_ax.text(
846
871
  0.005,
847
872
  0.3,
848
873
  f"Kurtosis: {scipy.stats.kurtosis(col_df):.2f}", # Excess Kurtosis
849
874
  fontdict=font_kws,
850
- transform=g.axes[0, 0].transAxes,
875
+ transform=plot_ax.transAxes,
851
876
  )
852
- g.axes[0, 0].text(
877
+ plot_ax.text(
853
878
  0.005,
854
879
  0.1,
855
880
  f"Count: {len(col_df)}",
856
881
  fontdict=font_kws,
857
- transform=g.axes[0, 0].transAxes,
882
+ transform=plot_ax.transAxes,
858
883
  )
859
- g.axes[0, 0].legend(loc="upper right")
884
+ plot_ax.legend(loc="upper right")
860
885
 
861
- return g.axes[0, 0]
886
+ return plot_ax
862
887
 
863
888
 
864
889
  def missingval_plot( # noqa: PLR0915
@@ -140,7 +140,7 @@ def _diff_report(
140
140
  )
141
141
  print(f"Dropped missing values: {data_mv_tot - data_cl_mv_tot}")
142
142
  mem_change = data_mem - data_cl_mem
143
- mem_perc = round(100 * mem_change / data_mem, 2)
143
+ mem_perc = round(100 * mem_change / data_mem, 2) if data_mem else 0
144
144
  print(f"Reduced memory by at least: {round(mem_change, 3)} MB (-{mem_perc}%)\n")
145
145
 
146
146
 
@@ -0,0 +1,5 @@
1
+ from __future__ import annotations
2
+
3
+ import matplotlib as mpl
4
+
5
+ mpl.use("Agg")
@@ -68,7 +68,7 @@ class Test_clean_column_names(unittest.TestCase):
68
68
  clean_column_names(self.df_clean_column_names, hints=True)
69
69
  sys.stdout = sys.__stdout__
70
70
  assert captured_output.getvalue() == (
71
- "(\"Duplicate column names detected! Columns with index [7, 8] and names ['dupli', 'also'] have been renamed to ['dupli_7', 'also_8'].\", \"Long column names detected (>25 characters). Consider renaming the following columns ['ae_some_plus_plus_dollar_percent_name', 'verylong_column_namesare_hardto_read'].\")\n"
71
+ "Duplicate column names detected! Columns with index [7, 8] and names ['dupli', 'also'] have been renamed to ['dupli_7', 'also_8']. Long column names detected (>25 characters). Consider renaming the following columns ['ae_some_plus_plus_dollar_percent_name', 'verylong_column_namesare_hardto_read'].\n"
72
72
  )
73
73
 
74
74
 
@@ -160,28 +160,24 @@ class Test_data_cleaning(unittest.TestCase):
160
160
 
161
161
  expected_results = ["string", "float32", "object", "object"]
162
162
  for i, _ in enumerate(expected_results):
163
- result_dtype = data_cleaning(self.df_data_cleaning, convert_dtypes=True).dtypes.iloc[i]
164
- assert result_dtype.name == expected_results[i]
163
+ assert (
164
+ data_cleaning(self.df_data_cleaning, convert_dtypes=True).dtypes.iloc[i]
165
+ == expected_results[i]
166
+ )
165
167
 
166
- expected_results = [
167
- ["str", "string", "object"],
168
- ["object"],
169
- ["object"],
170
- ["object"],
171
- ]
172
- for i, allowed in enumerate(expected_results):
173
- result_dtype = data_cleaning(self.df_data_cleaning, convert_dtypes=False).dtypes.iloc[i]
174
- assert result_dtype.name in allowed
168
+ expected_results = ["O", "O", "O", "O"]
169
+ for i, _ in enumerate(expected_results):
170
+ assert (
171
+ data_cleaning(self.df_data_cleaning, convert_dtypes=False).dtypes.iloc[i]
172
+ == expected_results[i]
173
+ )
175
174
 
176
- expected_results = [
177
- ["str", "string", "object"],
178
- ["object"],
179
- ["object"],
180
- ["object"],
181
- ]
182
- for i, allowed in enumerate(expected_results):
183
- result_dtype = data_cleaning(self.df_data_cleaning, convert_dtypes=False).dtypes.iloc[i]
184
- assert result_dtype.name in allowed
175
+ expected_results = ["O", "O", "O", "O"]
176
+ for i, _ in enumerate(expected_results):
177
+ assert (
178
+ data_cleaning(self.df_data_cleaning, convert_dtypes=False).dtypes.iloc[i]
179
+ == expected_results[i]
180
+ )
185
181
 
186
182
 
187
183
  class Test_convert_dtypes(unittest.TestCase):
@@ -208,8 +204,10 @@ class Test_convert_dtypes(unittest.TestCase):
208
204
  "category",
209
205
  ]
210
206
  for i, _ in enumerate(expected_results):
211
- result_dtype = convert_datatypes(self.df_data_convert, cat_threshold=0.4).dtypes.iloc[i]
212
- assert result_dtype.name == expected_results[i]
207
+ assert (
208
+ convert_datatypes(self.df_data_convert, cat_threshold=0.4).dtypes[i]
209
+ == expected_results[i]
210
+ )
213
211
 
214
212
  expected_results = [
215
213
  "int8",
@@ -2,10 +2,17 @@ from __future__ import annotations
2
2
 
3
3
  import unittest
4
4
 
5
+ import matplotlib.pyplot as plt
5
6
  import numpy as np
6
7
  import pandas as pd
8
+ import pytest
7
9
 
10
+ from klib.describe import cat_plot
11
+ from klib.describe import corr_interactive_plot
8
12
  from klib.describe import corr_mat
13
+ from klib.describe import corr_plot
14
+ from klib.describe import dist_plot
15
+ from klib.describe import missingval_plot
9
16
 
10
17
 
11
18
  class Test_corr_mat(unittest.TestCase):
@@ -116,3 +123,75 @@ class Test_corr_mat(unittest.TestCase):
116
123
  target=self.data_corr_target_list,
117
124
  colored=False,
118
125
  ).shape == (4, 1)
126
+
127
+
128
+ class Test_plots(unittest.TestCase):
129
+ def tearDown(self):
130
+ plt.close("all")
131
+
132
+ def test_cat_plot_smoke(self):
133
+ data = pd.DataFrame(
134
+ {
135
+ "cat1": ["a", "b", "a", "c", "a", "b"],
136
+ "cat2": ["x", "x", "y", "z", "z", "z"],
137
+ },
138
+ )
139
+
140
+ assert cat_plot(data, figsize=(4, 4)) is not None
141
+
142
+ def test_corr_plot_smoke(self):
143
+ data = pd.DataFrame(
144
+ {
145
+ "a": [1, 2, 3, 4, 5],
146
+ "b": [2, 4, 6, 8, 10],
147
+ "c": [5, 4, 3, 2, 1],
148
+ },
149
+ )
150
+
151
+ assert corr_plot(data, figsize=(4, 4)) is not None
152
+
153
+ def test_corr_plot_uses_existing_ax(self):
154
+ data = pd.DataFrame(
155
+ {
156
+ "a": [1, 2, 3, 4, 5],
157
+ "b": [2, 4, 6, 8, 10],
158
+ "c": [5, 4, 3, 2, 1],
159
+ },
160
+ )
161
+ _, ax = plt.subplots()
162
+
163
+ assert corr_plot(data, ax=ax) is ax
164
+
165
+ def test_corr_interactive_plot_smoke(self):
166
+ data = pd.DataFrame(
167
+ {
168
+ "a": [1, 2, 3, 4, 5],
169
+ "b": [2, 4, 6, 8, 10],
170
+ "c": [5, 4, 3, 2, 1],
171
+ },
172
+ )
173
+
174
+ assert corr_interactive_plot(data, figsize=(4, 4)) is not None
175
+
176
+ def test_dist_plot_smoke(self):
177
+ data = pd.DataFrame({"a": np.arange(30), "b": np.arange(30) ** 2})
178
+
179
+ assert dist_plot(data, size=2) is not None
180
+
181
+ def test_dist_plot_uses_existing_ax(self):
182
+ data = pd.Series(np.arange(30), name="a")
183
+ _, ax = plt.subplots()
184
+
185
+ assert dist_plot(data, ax=ax) is ax
186
+
187
+ def test_dist_plot_existing_ax_requires_single_column(self):
188
+ data = pd.DataFrame({"a": np.arange(30), "b": np.arange(30) ** 2})
189
+ _, ax = plt.subplots()
190
+
191
+ with pytest.raises(ValueError):
192
+ dist_plot(data, ax=ax)
193
+
194
+ def test_missingval_plot_smoke(self):
195
+ data = pd.DataFrame({"a": [1, np.nan, 3], "b": [np.nan, 2, 3]})
196
+
197
+ assert missingval_plot(data, figsize=(4, 4)) is not None