-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
COMPAT: Emit warning when groupby by a tuple #18731
Merged
TomAugspurger
merged 12 commits into
pandas-dev:master
from
TomAugspurger:warn-groupby-tuple
Dec 18, 2017
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
209ffce
COMPAT: Emit warning when groupby by a tuple
TomAugspurger ad09ade
DOC: avoid future warning
TomAugspurger a489b20
Merge remote-tracking branch 'upstream/master' into warn-groupby-tuple
TomAugspurger ade2b2b
Cleanup, test unhashable
TomAugspurger 6050226
PEP8
TomAugspurger d8c20e8
Correct KeyError
TomAugspurger d2a2372
update
TomAugspurger 38ef818
xfail
TomAugspurger a27f449
remove old comments
TomAugspurger 4e5ae9f
pep8
TomAugspurger a8b4383
Fixups
TomAugspurger 3057aab
Merge remote-tracking branch 'upstream/master' into warn-groupby-tuple
TomAugspurger 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 |
---|---|---|
|
@@ -2727,6 +2727,34 @@ def test_empty_dataframe_groupby(self): | |
|
||
assert_frame_equal(result, expected) | ||
|
||
def test_tuple_warns(self): | ||
# https://github.com/pandas-dev/pandas/issues/18314 | ||
df = pd.DataFrame({('a', 'b'): [1, 1, 2, 2], 'a': [1, 1, 1, 2], | ||
'b': [1, 2, 2, 2], 'c': [1, 1, 1, 1]}) | ||
with tm.assert_produces_warning(FutureWarning) as w: | ||
df[['a', 'b', 'c']].groupby(('a', 'b')).c.mean() | ||
|
||
assert "Interpreting tuple 'by' as a list" in str(w[0].message) | ||
|
||
with tm.assert_produces_warning(FutureWarning) as w: | ||
df[['a', 'b', 'c']].groupby(('a', 'b')).c.mean() | ||
|
||
assert "Interpreting tuple 'by' as a list" in str(w[0].message) | ||
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. Isn't this the same as above? |
||
|
||
with tm.assert_produces_warning(None): | ||
df.groupby(('a', 'b')).c.mean() | ||
|
||
def test_tuple_warns_unhashable(self): | ||
# https://github.com/pandas-dev/pandas/issues/18314 | ||
business_dates = date_range(start='4/1/2014', end='6/30/2014', | ||
freq='B') | ||
df = DataFrame(1, index=business_dates, columns=['a', 'b']) | ||
|
||
with tm.assert_produces_warning(FutureWarning) as w: | ||
df.groupby((df.index.year, df.index.month)).nth([0, 3, -1]) | ||
|
||
assert "Interpreting tuple 'by' as a list" in str(w[0].message) | ||
|
||
|
||
def _check_groupby(df, result, keys, field, f=lambda x: x.sum()): | ||
tups = lmap(tuple, df[keys].values) | ||
|
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.
I'm lost. Why do you check that elements are not hashable? I would have done instead
or (if we want to account for the to-be-deprecated possibility to index with missing keys):
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.
Or better - performance-wise:
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 is for the case where you're grouping by non-hashable arrays like in #18314 (comment)
In that case, don't we know that they're certainly relying on
groupby((a, b))
to begroupby([a, b])
, so we want to warn and listify?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.
I do still need to handle your KeyError example.
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.
Ahh, I see what you're doing now. Yes, that's probably better, and will make handling the KeyError easier.