Fix DataFrame and GroupBy.value_counts to match pandas ordering, dropna, and categorical handling - #22903
Conversation
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
DataFrame and GroupBy.value_counts to match pandas ordering, dropna, and categorical handling
|
/okay to test f588575 |
|
/okay to test 0b418b2 |
|
/okay to test 13fd44b |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughTwo independent changes: (1) Changesvalue_counts stable ordering
IntermediateProxy parent staleness tracking
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
|
/okay to test 4a8368a |
|
pre-commit.ci autofix |
|
/okay to test 8a7f906 |
|
/okay to test 239f1e5 |
|
/okay to test ca11e4d |
|
/okay to test 4f9c5e2 |
Per review, split the `_IntermediateProxy` parent-staleness fix (and its unit test plus the iloc xfail removals it unblocks) out of this PR; it now lives in NVIDIA#22998. This PR keeps only the DataFrame/GroupBy value_counts changes.
|
Thanks @mroeschke.
|
|
/okay to test 0bce1ba |
|
/merge |
|
@galipremsagar the pandas-tests failure here looks relevant. The other two issues are flaky ones. |
I have been seeing the same tests fail in other PRs when the following nvrtc error shows up in other jobs: Trying a rerun. |
|
/okay to test 82b6c87 |
|
/okay to test 02cee7e |
|
/merge |
|
/okay to test 7ea6954 |
|
/merge |
…22998) Split out of #22903 per review. ## Problem An `_IntermediateProxy` (e.g. a `groupby`, `rolling`, or accessor object) caches its wrapped object at creation time. When a parent proxy (the originating frame/series, or a proxy passed as an argument such as a grouping key) is mutated or replaced *after* the intermediate is created, the intermediate keeps returning the stale snapshot instead of reflecting the live parent. For example, a column added to a frame after a groupby was created is not visible through that groupby, which diverges from pandas' live-reference semantics: ```python df = xpd.DataFrame({"a": ["x", "y", "x", "y"], "b": [1, 2, 3, 4]}) gb = df.groupby(df["a"].values) # falls back to slow path -> proxy created in slow state df["c"] = df["b"] * 10 gb["c"].sum() # previously failed to see column "c" ``` ## Fix Snapshot the identity of each parent proxy's wrapped object when the intermediate is created, and on `_fsproxy_slow_to_fast` / `_fsproxy_fast_to_slow` re-derive the intermediate from the originating call when a parent has changed. Parent collection (`_collect_parent_proxies`) is intentionally iterative (an explicit stack) rather than a recursive closure: a recursive closure would hold a cell referencing itself, forming a reference cycle that delays reference-counted teardown and breaks code relying on prompt teardown (e.g. the pandas `Series.str`-accessor circular-reference test). ## Tests Adds `test_groupby_reflects_parent_frame_mutation`, and removes the now-passing `test_iloc_setitem_axis_argument[True/False]` xfails from the pandas-tests plugin. Authors: - GALI PREM SAGAR (https://github.com/galipremsagar) Approvers: - Vyas Ramasubramani (https://github.com/vyasr) URL: #22998
Description
Reworks
value_countsforDataFrame.value_countsandDataFrameGroupBy/SeriesGroupBy.value_countsso cudf matches pandas, and fixes a relatedcudf.pandasproxy bug. Together these resolve a number of pandas-compatibility test failures intests/groupby/methods/test_value_counts.py.Ordering of tied counts (
DataFrameand groupbyvalue_counts)cudf's groupby does not preserve first-appearance order, so rows with equal counts were ordered differently from pandas. Both code paths now track the first row index of each unique combination and order by it, with a running sequence column that keeps the optional value sort (
sort=) and the group-key sort stable for ties — cudf's value sort is not guaranteed stable.GroupBy.value_countscorrectnessdropna: the groupbydropna(group keys) and thevalue_countsdropna(subset) are now applied independently, matching pandas, by grouping withdropna=Falseand filtering afterward.0). cudf's groupby only emits observed combinations, so the result is expanded to the full product of the observed group keys × subset categories.Grouper(freq=...)keys: the grouping's actual (binned) key values are used instead of the raw object columns, so a frequency grouper that floors its key produces the correct group labels.normalize(divide each count by its per-group total) andas_index=Falseoutput.The now-passing array-keyed, categorical, and time-grouper
test_value_countscases are removed from thecudf.pandasxfail list.Checklist