Repository navigation
[Fix] Match pandas .dt values on polars and resolve to_datetime options - #253
Merged
Merged
Conversation
polars counts weekdays from Monday=1 and nanoseconds past the second, while pandas counts from Monday=0 and only 0..999 past the microsecond, so polars plans returned different values for .dt.dayofweek, .dt.weekday and .dt.nanosecond. PolarsAccessorMapOp and DtExpr.to_polars now share polars_dt_attribute, which corrects both, and the name map gains the pandas aliases polars lacks (day_of_week, day_of_year, daysinmonth). Both DatetimeConversionOp impls passed self.args/self.kwargs through unresolved, so a plan-fed option such as a computed format reached pd.to_datetime or the polars translation as an OperandRef and crashed. They now resolve through _extract_args_and_kwargs like the other CallMapOp impls.
Codecov Report✅ All modified and coverable lines are covered by tests.
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
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.
Summary
Two correctness bugs in the polars/pandas map impls.
1. polars
.dtreads returned different values than pandas. polars counts weekdays from Monday=1 and nanoseconds past the second; pandas counts from Monday=0 and only 0..999 past the microsecond. Both polars paths (PolarsAccessorMapOpandDtExpr.to_polars, used byAssignMapOpand mask selections) now go through one shared helper, likepolars_string_methodfor.str:POLARS_ATTR_NAME_MAPalso gains the pandas aliases polars lacks:day_of_week,day_of_year,daysinmonth. Before, these crashed inPolarsAccessorMapOpand refused polars inDtExpr.2.
pd.to_datetimeignored plan-fed options. BothDatetimeConversionOpimpls passedself.args/self.kwargsstraight through, so an option computed in the plan reached pandas/polars as an unresolvedOperandRef. They now resolve through_extract_args_and_kwargs(inputs)like the otherCallMapOpimpls, sopolars_datetime_kwargssees the real values.Evidence
main.After: polars returns
[0, 6], and both backends parse with the computed format.New tests in
tests/optimizer/logical/test_map_ops.py:test_dt_attribute_matches_pandas[attr-path-backend]:dayofweek,day_of_week,weekday,nanosecond,day_of_year,daysinmonthx standalone accessor / assign / mask selection x pandas / polars. Each case checks the result against eager pandas, and on polars checks that the result is a polars object, so a pandas fallback cannot hide the bug.test_to_datetime_resolves_graph_fed_format[pandas|polars]: the repro above, run end to end.TestDatetimeConversionOp.test_resolves_graph_fed_kwarg/_positional:OperandRefoptions at the op level, on both backends.TestAccessorMapOp.test_polars_matches_pandas_valuesreplacestest_polars_dayofweek, which asserted the off-by-one value.Full suite locally: 1914 passed, 13 failed. The same 13 fail on a clean checkout of
main. They are thetests/adaptersencoders and the random forest histogram tests, which need the compiled Rust runtime.