-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
REGR: groupby.idxmin/idxmax wrong result on extreme values #57046
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7063fce
WIP
rhshadrach 866b581
REGR: groupby.idxmin/idxmax wrong result on extreme values
rhshadrach 351bf69
Merge branch 'reg_groupby_idxmin' of https://github.com/rhshadrach/pa…
rhshadrach 2dfe3a4
fixes
rhshadrach 9c54b8e
NumPy 2.0 can't come soon enough
rhshadrach b424a5b
Merge remote-tracking branch 'upstream/main' into reg_groupby_idxmin
rhshadrach d2dda0a
fixup
rhshadrach cf22b81
fixup
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 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 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 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -257,6 +257,69 @@ def test_empty(frame_or_series, all_boolean_reductions): | |||||
tm.assert_equal(result, expected) | ||||||
|
||||||
|
||||||
@pytest.mark.parametrize("how", ["idxmin", "idxmax"]) | ||||||
def test_idxmin_idxmax_extremes(how, any_real_numpy_dtype): | ||||||
# GH#57040 | ||||||
if any_real_numpy_dtype is int or any_real_numpy_dtype is float: | ||||||
# No need to test | ||||||
return | ||||||
info = np.iinfo if "int" in any_real_numpy_dtype else np.finfo | ||||||
min_value = info(any_real_numpy_dtype).min | ||||||
max_value = info(any_real_numpy_dtype).max | ||||||
df = DataFrame( | ||||||
{"a": [2, 1, 1, 2], "b": [min_value, max_value, max_value, min_value]}, | ||||||
dtype=any_real_numpy_dtype, | ||||||
) | ||||||
gb = df.groupby("a") | ||||||
result = getattr(gb, how)() | ||||||
expected = DataFrame( | ||||||
{"b": [1, 0]}, index=pd.Index([1, 2], name="a", dtype=any_real_numpy_dtype) | ||||||
) | ||||||
tm.assert_frame_equal(result, expected) | ||||||
|
||||||
|
||||||
@pytest.mark.parametrize("how", ["idxmin", "idxmax"]) | ||||||
@pytest.mark.parametrize("dtype", ["float32", "float64"]) | ||||||
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.
|
||||||
def test_idxmin_idxmax_extremes_skipna(skipna, how, dtype): | ||||||
# GH#57040 | ||||||
min_value = np.finfo(dtype).min | ||||||
max_value = np.finfo(dtype).max | ||||||
df = DataFrame( | ||||||
{ | ||||||
"a": np.repeat(range(1, 6), repeats=2), | ||||||
"b": Series( | ||||||
[ | ||||||
np.nan, | ||||||
min_value, | ||||||
np.nan, | ||||||
max_value, | ||||||
min_value, | ||||||
np.nan, | ||||||
max_value, | ||||||
np.nan, | ||||||
np.nan, | ||||||
np.nan, | ||||||
], | ||||||
dtype=dtype, | ||||||
), | ||||||
}, | ||||||
) | ||||||
gb = df.groupby("a") | ||||||
|
||||||
warn = None if skipna else FutureWarning | ||||||
msg = f"The behavior of DataFrameGroupBy.{how} with all-NA values" | ||||||
with tm.assert_produces_warning(warn, match=msg): | ||||||
result = getattr(gb, how)(skipna=skipna) | ||||||
if skipna: | ||||||
values = [1, 3, 4, 6, np.nan] | ||||||
else: | ||||||
values = np.nan | ||||||
print(df) | ||||||
print(result) | ||||||
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.
Suggested change
|
||||||
expected = DataFrame({"b": values}, index=pd.Index(range(1, 6), name="a")) | ||||||
tm.assert_frame_equal(result, expected) | ||||||
|
||||||
|
||||||
@pytest.mark.parametrize( | ||||||
"func, values", | ||||||
[ | ||||||
|
Oops, something went wrong.
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.
Why is kwargs needed here?
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.
To pass skipna to group_idxmin_idxmax
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.
Ah OK and I guess not all these accept skipna (IIRC there was that related issue related
skipna
, but forgot if it was for groupby ops or not)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.
Yea - I'm planning on turning #56939 into a tracking issue on adding skipna consistently throughout groupby. Can include removing these kwargs as part of that.