feat(datastore): mirror pandas iteration on LazyGroupBy / SeriesGroupBy#582
Merged
Merged
Conversation
5654ef1 to
5ae7dee
Compare
5ae7dee to
d90743f
Compare
… only transform() and similar groupby-context methods re-copy _expr=Field and _groupby_fields onto an op-mode result. The previous judgement _groupby_fields and isinstance(_expr, Field) mis-classified them as SeriesGroupBy and yielded (key, sub_series) pairs instead of transform values. Require also _source / _op_type / _agg_func_name to all be None so only the pure ds.groupby(...)[col] ColumnExpr triggers SeriesGroupBy iter. Regression test in test_groupby_iteration.py covers groupby + transform.
The 4 dropna+NaN tests were dropped in earlier commits to unblock CI on pandas 2.x, which has version-specific bugs: - get_group(np.nan) / get_group((x, np.nan)) raise KeyError (NaN!=NaN in hash lookup), fixed in pandas 3.x - groupby(NaN-col, dropna=False).groups raises ValueError 'Categorical categories cannot be null', fixed in pandas 3.x DataStore mirrors pandas via _pandas_groupby(), so it inherits whatever pandas does. Skip-on-pandas2 keeps pandas 3.x coverage instead of losing the tests entirely.
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.
Closes #581.
Summary
LazyGroupByhad no__iter__, sofor k, g in ds.groupby([...]):fell back to__getitem__(0)and raisedTypeError: Expected str or list, got int— a misleading message hiding a missing feature.ColumnExpr.__iter__also dropped groupby context:for x in gb['col']:yielded raw values instead of pandasSeriesGroupBy(key, sub_series)pairs.Changes
datastore/groupby.py—LazyGroupByadds__iter__,__len__,__contains__,get_group(name, obj=None),groups,indices. All delegate to a shared_pandas_groupby()helper that respectssort/dropna/selected_columns; single-col groupby uses scalarbyto match thegb['col']scalar-key convention.ngroupsrefactored onto the helper too.datastore/column_expr.py—__iter__yields(key, sub_series)when_groupby_fieldsis set; plainds['col']iteration unchanged.__getitem__errors — clearerTypeErrorforgb[<int>](suggests iter /get_group) andgb[<other>](suggestsstr/ list ofstr).Tests
datastore/tests/test_groupby_iteration.py— 44 mirror-pattern tests across 10TestCases:sort=True/False,dropna=True/False,as_index=False, column selectionget_groupsingle / multi / column-selected / missing-key / legacyobj=kwarg.groups/.indicesfor single & multi columnlen(gb)/key in gbSeriesGroupByiter + clear error for iter on computed expressionsgb[<int|float|None>]error path coverageAll 44 pass locally. Verified no regression on adjacent
column_expr/groupbytest files (178 passed).Verification