-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fix remaining cases of groupby(...).transform with dropna=True #46367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2143c59
BUG: Correct results for groupby(...).transform with null keys
rhshadrach deb3edb
Merge branch 'main' of https://github.com/pandas-dev/pandas into tran…
rhshadrach 04a2a6b
Series tests
rhshadrach ad7bf3e
Remove reliance on Series
rhshadrach 39e1438
Merge branch 'main' of https://github.com/pandas-dev/pandas into tran…
rhshadrach 7a4a99b
Merge branch 'main' of https://github.com/pandas-dev/pandas into tran…
rhshadrach f234a89
Merge branch 'main' of https://github.com/pandas-dev/pandas into tran…
rhshadrach d1eeb65
Merge cleanup - WIP
rhshadrach e57025d
Merge branch 'transform_dropna' of https://github.com/rhshadrach/pand…
rhshadrach be2f45c
Fixes for ngroup
rhshadrach 8e5df01
Merge branch 'main' of https://github.com/pandas-dev/pandas into tran…
rhshadrach 2d79a0f
name in some cases
rhshadrach e2f3080
Merge branch 'main' of https://github.com/pandas-dev/pandas into tran…
rhshadrach d9da864
Fixups for ngroup
rhshadrach b973af0
Fixups
rhshadrach 67ffb60
Fixups
rhshadrach e31041d
Fixups
rhshadrach 60e60b0
Merge branch 'main' of https://github.com/pandas-dev/pandas into tran…
rhshadrach 04293a7
PR feedback
rhshadrach 9a6dc3c
whatsnew improvements
rhshadrach d7bb828
Merge branch 'main' of https://github.com/pandas-dev/pandas into tran…
rhshadrach 6571291
merge cleanup
rhshadrach a8f524b
type-hint fixup
rhshadrach 2a02de0
Merge branch 'main' of https://github.com/pandas-dev/pandas into tran…
rhshadrach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,6 +168,10 @@ def test_transform_axis_1(request, transformation_func): | |
# TODO(2.0) Remove after pad/backfill deprecation enforced | ||
transformation_func = maybe_normalize_deprecated_kernels(transformation_func) | ||
|
||
if transformation_func == "ngroup": | ||
msg = "ngroup fails with axis=1: #45986" | ||
request.node.add_marker(pytest.mark.xfail(reason=msg)) | ||
|
||
warn = None | ||
if transformation_func == "tshift": | ||
warn = FutureWarning | ||
|
@@ -383,6 +387,15 @@ def test_transform_transformation_func(request, transformation_func): | |
elif transformation_func == "fillna": | ||
test_op = lambda x: x.transform("fillna", value=0) | ||
mock_op = lambda x: x.fillna(value=0) | ||
elif transformation_func == "ngroup": | ||
test_op = lambda x: x.transform("ngroup") | ||
counter = -1 | ||
|
||
def mock_op(x): | ||
nonlocal counter | ||
counter += 1 | ||
return Series(counter, index=x.index) | ||
|
||
elif transformation_func == "tshift": | ||
msg = ( | ||
"Current behavior of groupby.tshift is inconsistent with other " | ||
|
@@ -394,10 +407,14 @@ def test_transform_transformation_func(request, transformation_func): | |
mock_op = lambda x: getattr(x, transformation_func)() | ||
|
||
result = test_op(df.groupby("A")) | ||
groups = [df[["B"]].iloc[:4], df[["B"]].iloc[4:6], df[["B"]].iloc[6:]] | ||
expected = concat([mock_op(g) for g in groups]) | ||
# pass the group in same order as iterating `for ... in df.groupby(...)` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you link the issue/PR here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Within this test, the issue number is on L374. |
||
# but reorder to match df's index since this is a transform | ||
groups = [df[["B"]].iloc[4:6], df[["B"]].iloc[6:], df[["B"]].iloc[:4]] | ||
expected = concat([mock_op(g) for g in groups]).sort_index() | ||
# sort_index does not preserve the freq | ||
expected = expected.set_axis(df.index) | ||
|
||
if transformation_func == "cumcount": | ||
if transformation_func in ("cumcount", "ngroup"): | ||
tm.assert_series_equal(result, expected) | ||
else: | ||
tm.assert_frame_equal(result, expected) | ||
|
@@ -1122,10 +1139,6 @@ def test_transform_agg_by_name(request, reduction_func, obj): | |
func = reduction_func | ||
g = obj.groupby(np.repeat([0, 1], 3)) | ||
|
||
if func == "ngroup": # GH#27468 | ||
request.node.add_marker( | ||
pytest.mark.xfail(reason="TODO: g.transform('ngroup') doesn't work") | ||
) | ||
if func == "corrwith" and isinstance(obj, Series): # GH#32293 | ||
request.node.add_marker( | ||
pytest.mark.xfail(reason="TODO: implement SeriesGroupBy.corrwith") | ||
|
@@ -1137,8 +1150,8 @@ def test_transform_agg_by_name(request, reduction_func, obj): | |
# this is the *definition* of a transformation | ||
tm.assert_index_equal(result.index, obj.index) | ||
|
||
if func != "size" and obj.ndim == 2: | ||
# size returns a Series, unlike other transforms | ||
if func not in ("ngroup", "size") and obj.ndim == 2: | ||
# size/ngroup return a Series, unlike other transforms | ||
tm.assert_index_equal(result.columns, obj.columns) | ||
|
||
# verify that values were broadcasted across each group | ||
|
@@ -1312,7 +1325,7 @@ def test_null_group_lambda_self(sort, dropna): | |
|
||
def test_null_group_str_reducer(request, dropna, reduction_func): | ||
# GH 17093 | ||
if reduction_func in ("corrwith", "ngroup"): | ||
if reduction_func == "corrwith": | ||
msg = "incorrectly raises" | ||
request.node.add_marker(pytest.mark.xfail(reason=msg)) | ||
index = [1, 2, 3, 4] # test transform preserves non-standard index | ||
|
@@ -1358,31 +1371,11 @@ def test_null_group_str_reducer(request, dropna, reduction_func): | |
|
||
|
||
@pytest.mark.filterwarnings("ignore:tshift is deprecated:FutureWarning") | ||
def test_null_group_str_transformer( | ||
request, using_array_manager, dropna, transformation_func | ||
): | ||
def test_null_group_str_transformer(request, dropna, transformation_func): | ||
# GH 17093 | ||
xfails_block = ( | ||
"cummax", | ||
"cummin", | ||
"cumsum", | ||
"fillna", | ||
"rank", | ||
"backfill", | ||
"ffill", | ||
"bfill", | ||
"pad", | ||
) | ||
xfails_array = ("cummax", "cummin", "cumsum", "fillna", "rank") | ||
if transformation_func == "tshift": | ||
msg = "tshift requires timeseries" | ||
request.node.add_marker(pytest.mark.xfail(reason=msg)) | ||
elif dropna and ( | ||
(not using_array_manager and transformation_func in xfails_block) | ||
or (using_array_manager and transformation_func in xfails_array) | ||
): | ||
msg = "produces incorrect results when nans are present" | ||
request.node.add_marker(pytest.mark.xfail(reason=msg)) | ||
args = (0,) if transformation_func == "fillna" else () | ||
df = DataFrame({"A": [1, 1, np.nan], "B": [1, 2, 2]}, index=[1, 2, 3]) | ||
gb = df.groupby("A", dropna=dropna) | ||
|
@@ -1420,10 +1413,6 @@ def test_null_group_str_reducer_series(request, dropna, reduction_func): | |
msg = "corrwith not implemented for SeriesGroupBy" | ||
request.node.add_marker(pytest.mark.xfail(reason=msg)) | ||
|
||
if reduction_func == "ngroup": | ||
msg = "ngroup fails" | ||
request.node.add_marker(pytest.mark.xfail(reason=msg)) | ||
|
||
# GH 17093 | ||
index = [1, 2, 3, 4] # test transform preserves non-standard index | ||
ser = Series([1, 2, 2, 3], index=index) | ||
|
@@ -1470,15 +1459,6 @@ def test_null_group_str_transformer_series(request, dropna, transformation_func) | |
if transformation_func == "tshift": | ||
msg = "tshift requires timeseries" | ||
request.node.add_marker(pytest.mark.xfail(reason=msg)) | ||
elif dropna and transformation_func in ( | ||
"cummax", | ||
"cummin", | ||
"cumsum", | ||
"fillna", | ||
"rank", | ||
): | ||
msg = "produces incorrect results when nans are present" | ||
request.node.add_marker(pytest.mark.xfail(reason=msg)) | ||
args = (0,) if transformation_func == "fillna" else () | ||
ser = Series([1, 2, 2], index=[1, 2, 3]) | ||
gb = ser.groupby([1, 1, np.nan], dropna=dropna) | ||
|
@@ -1502,4 +1482,10 @@ def test_null_group_str_transformer_series(request, dropna, transformation_func) | |
msg = f"{transformation_func} is deprecated" | ||
with tm.assert_produces_warning(warn, match=msg): | ||
result = gb.transform(transformation_func, *args) | ||
tm.assert_equal(result, expected) | ||
if dropna and transformation_func == "fillna": | ||
# GH#46369 - result name is the group; remove this block when fixed. | ||
tm.assert_equal(result, expected, check_names=False) | ||
# This should be None | ||
assert result.name == 1.0 | ||
else: | ||
tm.assert_equal(result, expected) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would this be a matter of supporting a mask arg in the libgroupby function, or would this be just for Nullable dtype? bc i have a branch on deck that does the former
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The former - I believe rank had to be singled out here because it was the one transform that didn't support a mask arg.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rhshadrach mask just got added to group_rank, but commenting this out causes a few failures. is more needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment was incorrect; opened #46953