-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Groupby quantiles incorrect bins #33200 #33644
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 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
15a27ea
BUG: Maintain the order of the bins in group_quantile. Updated tests …
mabelvj 8e6af0e
BUG: Fix quantile calculation #33200
mabelvj 5832ba9
BUG: Fix quantile calculation. Only move -1 labels if there are any l…
mabelvj 662c102
Merge branch 'master' into 33200-groupby-quantile
mabelvj e70e3ca
BUG: Explicit quantile to 0.5. Fix format #33200
mabelvj b0c309b
BUG: Fix whatsnew. #33200
mabelvj 131a8c1
BUG: Fix merge master. #33200
mabelvj d18bdc4
Merge remote-tracking branch 'upstream/master' into 33200-groupby-qua…
simonjayhawkins e7c0eac
troubleshoot ci timeouts
simonjayhawkins 07fa080
issue number and coments
simonjayhawkins 24779c6
Merge remote-tracking branch 'upstream/master' into 33200-groupby-qua…
simonjayhawkins c6f41a4
add more test cases
simonjayhawkins 6b28d91
Merge remote-tracking branch 'upstream/master' into 33200-groupby-qua…
simonjayhawkins f21e332
update test per comment
simonjayhawkins 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -181,14 +181,23 @@ def test_quantile_missing_group_values_no_segfaults(): | |
grp.quantile() | ||
|
||
|
||
def test_quantile_missing_group_values_correct_results(): | ||
# GH 28662 | ||
data = np.array([1.0, np.nan, 3.0, np.nan]) | ||
df = pd.DataFrame(dict(key=data, val=range(4))) | ||
@pytest.mark.parametrize( | ||
"key, val, expected_key, expected_val", | ||
[ | ||
([1.0, np.nan, 3.0, np.nan], range(4), [1.0, 3.0], [0.0, 2.0]), | ||
([1.0, np.nan, 2.0, 2.0], range(4), [1.0, 2.0], [0.0, 2.5]), | ||
(["a", "b", "b", np.nan], range(4), ["a", "b"], [0, 1.5]), | ||
], | ||
) | ||
def test_quantile_missing_group_values_correct_results( | ||
key, val, expected_key, expected_val | ||
): | ||
# GH 28662, GH 33200, GH 33569 | ||
df = pd.DataFrame({"key": key, "val": val}) | ||
|
||
result = df.groupby("key").quantile() | ||
result = df.groupby("key").quantile(0.5) | ||
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 add another test that does not explicitly set a quantile (e.g. like the original) |
||
expected = pd.DataFrame( | ||
[1.0, 3.0], index=pd.Index([1.0, 3.0], name="key"), columns=["val"] | ||
expected_val, index=pd.Index(expected_key, name="key"), columns=["val"] | ||
) | ||
tm.assert_frame_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.
What is this required for? Do we have a test case when this is False?
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.
Normally, if labels are not provided or the dataframe is empty, an error message will rise when applying the quantile. However, there are cases where certain operations lead to empty labels, as when time resampling some types of empty dataframe:
Here is an example of the pipeline:
https://dev.azure.com/pandas-dev/pandas/_build/results?buildId=34258&view=logs&j=bef1c175-2c1b-51ae-044a-2437c76fc339&t=770e7bb1-09f5-5ebf-b63b-578d2906aac9&l=127
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.
Is this accounted for in the test that you've created? If not, can you add a test for it?
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.
add two cases that take the if labels.any(): is False path.
otherwise labels.max() can raise
ValueError: zero-size array to reduction operation maximum which has no identity