Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,39 @@ By default, `CleanConfig(strict_backend=True)` refuses to run any operation that

---

## Known differences between pandas and Polars

The two backends give different results in these cases. `tests/test_known_differences.py` checks each row.

| Case | pandas | Polars |
| :--- | :--- | :--- |
| Currency strings such as `"$1,200"` | Plans `parse_currency` and gives `1200.0` | Keeps the string |
| Blank strings (`""`, `" "`) | `strip_strings` turns them into nulls | `strip_strings` keeps `""` |
| Fill values and IQR fences | Computed after earlier steps such as `drop_nulls` and `drop_duplicates` | Computed on the input frame |
| Row order after `drop_duplicates` | Keeps the first row of each group, in order | No fixed order (`unique(maintain_order=False)`) |
| Replaying a plan on new data | Uses the fill value stored in the plan | Computes the fill value again on the new data |
| `CleanReport` for a LazyFrame | Not applicable | `original_rows` and `cleaned_rows` are `0`, because freshdata does not run a query to count rows |

If you need the same result on both backends, use the same backend for planning and applying, and check the plan before you apply it.

---

## Known differences between pandas and Polars

The two backends do not give the same result in these cases.
`tests/test_known_differences.py` checks that each row is still true.

| Case | pandas | Polars |
|---|---|---|
| Currency strings such as `"$1,200"` | Parses them to `float64` (`parse_currency`) | Keeps them as strings |
| Blank strings such as `""` or `" "` | Strips them to null | Strips them to `""` |
| Fill values and IQR fences | Computed after earlier actions, for example after `drop_nulls` or `drop_duplicates` | Computed on the input frame, in one planning query |
| Replaying a plan on new data | Uses the fill value stored in the plan | Computes the fill value again on the new data |
| `report.original_rows` and `report.cleaned_rows` for a `LazyFrame` | Not applicable | `0`, because counting rows would read the data |
| Row order after `drop_duplicates` | Keeps the first row of each group, in order | Does not keep the order (`maintain_order=False`) |

---

## Upgrading from v2.x

If you are upgrading from `freshdata` 2.x (`freshdata-cleaner`), run the included `libcst` codemod to rewrite legacy wrappers (`clean_csv`, `fill_missing`, `compare_plans`, etc.) into the v3.0 core API:
Expand Down
73 changes: 73 additions & 0 deletions tests/test_known_differences.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""The README lists where pandas and Polars give different results.

Each test checks that the README still names a difference and that the
difference still exists. If you remove a difference, this test fails:
then remove its README row too.
"""

import pathlib

import pandas as pd
import pytest

import freshdata as fd

pl = pytest.importorskip("polars")

README = (pathlib.Path(__file__).resolve().parents[1] / "README.md").read_text(encoding="utf-8")
SECTION = README.split("## Known differences between pandas and Polars", 1)[-1].split("\n## ", 1)[0]


def _both(data, cfg=None):
cfg = cfg or fd.CleanConfig()
pdf, pldf = pd.DataFrame(data), pl.DataFrame(data)
return (fd.apply_plan(pdf, fd.suggest_plan(pdf, cfg), cfg),
fd.apply_plan(pldf, fd.suggest_plan(pldf, cfg), cfg))


def test_readme_has_the_section():
assert "## Known differences between pandas and Polars" in README


def test_currency_strings():
assert "Currency strings" in SECTION
pd_out, pl_out = _both({"price": ["$1,200", "$30"]})
assert pd_out["price"].tolist() == [1200.0, 30.0]
assert pl_out["price"].to_list() == ["$1,200", "$30"]


def test_blank_strings():
assert "Blank strings" in SECTION
pd_out, pl_out = _both({"name": [" a ", "", " "]})
assert pd_out["name"].isna().tolist() == [False, True, True]
assert pl_out["name"].to_list() == ["a", "", ""]


def test_fill_values_and_fences_see_the_input_frame_on_polars():
assert "Fill values and IQR fences" in SECTION
data = {"a": [1.0, None, None, 4.0], "b": [None, 100.0, 100.0, 2.0]}
pd_out, pl_out = _both(data, fd.CleanConfig(rules={"a": fd.ColumnRule(impute="drop")}))
assert pd_out["b"].tolist() == [2.0, 2.0] # median of b after the drop
assert pl_out["b"].to_list() == [100.0, 2.0] # median of b in the input


def test_replaying_a_plan_on_new_data():
assert "Replaying a plan" in SECTION
old, new = {"x": [1.0, 2.0, None]}, {"x": [100.0, 200.0, None]}
assert fd.apply_plan(pd.DataFrame(new), fd.suggest_plan(pd.DataFrame(old)))["x"].tolist()[-1] == 1.5
assert fd.apply_plan(pl.DataFrame(new), fd.suggest_plan(pl.DataFrame(old)))["x"].to_list()[-1] == 150.0


def test_lazyframe_report_row_counts():
assert "`original_rows`" in SECTION
lf = pl.LazyFrame({"x": [1.0, None]})
_, report = fd.apply_plan(lf, fd.suggest_plan(lf), return_report=True)
assert (report.original_rows, report.cleaned_rows) == (0, 0)


def test_row_order_after_drop_duplicates():
assert "Row order after `drop_duplicates`" in SECTION
data, cfg = {"k": [3, 1, 3, 2, 1]}, fd.CleanConfig(drop_duplicates=True)
assert fd.apply_plan(pd.DataFrame(data), fd.suggest_plan(pd.DataFrame(data), cfg), cfg)["k"].tolist() == [3, 1, 2]
lf = pl.LazyFrame(data)
assert "UNIQUE[maintain_order: false" in fd.apply_plan(lf, fd.suggest_plan(lf, cfg), cfg).explain()
Loading