Repository navigation
fix(polars): plan missing-value actions only where pandas does - #542
Merged
Merged
Conversation
The Polars planner added impute_median for every numeric column, also for columns with no nulls. The fill used a Float64 median, so applying the plan turned Int64 columns into Float64. pandas skips those columns. The planner now gets one keep flag per missing-value action from its one planning query and drops the actions that change no cell: a fill needs some nulls but not only nulls, drop_nulls needs nulls (or an empty frame), ffill needs one filled cell. The rules match stage_missing in pandas. Int columns with no nulls stay Int64. The README output loses the no-op "invoice_total: impute_median" line.
Contributor
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cause
The Polars planner planned
impute_medianfor every numeric column, also for columns with no nulls.apply_planfills withexpr.median(), which is Float64. So anInt64column with no nulls came back asFloat64. pandas plans no fill for that column, so it staysint64. The README quickstart showed the symptom:[missing] invoice_total: impute_median {}for a column with no nulls.This is launch gate G6 in the adoption audit (Phase 3 parity difference P1).
Change
PolarsBackend.suggest_planadds one boolean "keep" flag per missing-value action to its one planning query. After the query, it drops the actions that change no cell. The rules matchstage_missingin pandas:impute_median,impute_mean,impute_mode: the column has some nulls, but not only nulls.impute_ffill: forward fill fills at least one cell.drop_nulls: the column has nulls, or the frame is empty.The planner still runs one query.
apply_planstill runs no query.Check
New tests in
tests/test_backend_honesty.py:test_polars_plans_missing_actions_only_where_pandas_does(5 strategies,DataFrameandLazyFrame)test_polars_keeps_int_dtype_when_no_value_is_missingBefore (main
c386c8d):After:
Full suite:
107 passed, coverage 96.18%.ruff check .andmypyare clean. The README blocks run with exit 0.Public behavior change
Polars plans have fewer actions. Columns with no nulls (or only nulls) get no missing-value action, and
Int64columns stayInt64afterapply_plan. pandas plans do not change.CHANGELOG.mdrecords this under[3.0.0]/ Fixed, because 3.0.0 is not on PyPI yet.Not changed: after a
drop_nulls, pandas countsrows_affectedon the rows that are left, and Polars counts on the input. The new test compares actions, not those counts.