|
| 1 | +"""Column labels that pandas itself handles awkwardly: missing, coerced, repeated. |
| 2 | +
|
| 3 | +Regressions for #459 (suggest_plan on duplicate labels), #461 (numeric-or-None |
| 4 | +labels), #462 (label identity in infer_roles) and #437 (the text entry points). |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import warnings |
| 10 | + |
| 11 | +import pandas as pd |
| 12 | +import pytest |
| 13 | + |
| 14 | +import freshdata as fd |
| 15 | + |
| 16 | +warnings.simplefilter("ignore") |
| 17 | + |
| 18 | + |
| 19 | +# ── #461: a NaN column label must not break duplicate detection ──────────────── |
| 20 | + |
| 21 | + |
| 22 | +def _nan_label_frame() -> pd.DataFrame: |
| 23 | + # pandas coerces Index([0, None]) to float64, so the second label is NaN and |
| 24 | + # cannot be looked up by value — DataFrame.duplicated() raised KeyError. |
| 25 | + return pd.DataFrame({0: [1, 2, 1], None: [3, 4, 3]}) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize("call", [ |
| 29 | + lambda df: fd.clean(df, verbose=False), |
| 30 | + fd.profile, |
| 31 | + fd.infer_roles, |
| 32 | + fd.explain_clean, |
| 33 | +]) |
| 34 | +def test_numeric_or_none_labels_are_accepted(call): |
| 35 | + assert call(_nan_label_frame()) is not None |
| 36 | + |
| 37 | + |
| 38 | +def test_duplicate_rows_are_still_detected_with_a_nan_label(): |
| 39 | + _, report = fd.clean( |
| 40 | + _nan_label_frame(), drop_duplicates=True, return_report=True, verbose=False |
| 41 | + ) |
| 42 | + assert any(a.step == "drop_duplicates" and a.count == 1 for a in report.actions) |
| 43 | + |
| 44 | + |
| 45 | +def test_duplicate_subset_still_applies_with_a_nan_label(): |
| 46 | + df = pd.DataFrame({0: [1, 1, 2], None: [9, 8, 7]}) |
| 47 | + out = fd.clean( |
| 48 | + df, drop_duplicates=True, duplicate_subset=[0.0], verbose=False |
| 49 | + ) |
| 50 | + assert len(out) == 2 # deduplicated on the first column only |
| 51 | + |
| 52 | + |
| 53 | +# ── #462: infer_roles reports the labels the frame actually has ──────────────── |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.parametrize("labels", [[0, None], [-2, 0.78], ["a", 1]]) |
| 57 | +def test_infer_roles_keeps_label_identity(labels): |
| 58 | + df = pd.DataFrame([[1, 2], [3, 4]]) |
| 59 | + df.columns = pd.Index(labels, dtype=object) |
| 60 | + reported = fd.infer_roles(df)["column"].tolist() |
| 61 | + assert reported == sorted(labels, key=str) # rows are ordered by label text |
| 62 | + for label in reported: |
| 63 | + assert df[label].shape == (2,) # the documented round-trip |
| 64 | + |
| 65 | + |
| 66 | +# ── #459 / #437: duplicate labels raise the same error everywhere ────────────── |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.parametrize("call", [ |
| 70 | + fd.suggest_plan, |
| 71 | + fd.plan, |
| 72 | + fd.clean_text, |
| 73 | + fd.lint_text_encoding, |
| 74 | + fd.infer_roles, |
| 75 | +]) |
| 76 | +def test_duplicate_labels_raise_value_error(call): |
| 77 | + df = pd.DataFrame([[1, 2], [3, 4]], columns=["a", "a"]) |
| 78 | + with pytest.raises(ValueError, match="requires unique column labels"): |
| 79 | + call(df) |
| 80 | + assert df.columns.tolist() == ["a", "a"] # never modified |
| 81 | + |
| 82 | + |
| 83 | +def test_unique_labels_still_work_on_those_entry_points(): |
| 84 | + df = pd.DataFrame({"a": [" x ", "y"], "n": [1, 2]}) |
| 85 | + assert fd.suggest_plan(df) is not None |
| 86 | + assert fd.plan(df) is not None |
| 87 | + cleaned, _ = fd.clean_text(df) |
| 88 | + assert cleaned["a"].tolist() == ["x", "y"] |
| 89 | + assert fd.lint_text_encoding(df) is not None |
0 commit comments