-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: group with multiple named results #21171
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5b99a11
add fix for bug 19029
guenteru 117872f
update old testcase to satisfy new behavior
guenteru 32e44c3
add additional groupby testcases (19029)
guenteru c2a3fa5
resolve flake8 conflicts
guenteru 7cd448a
change groupby-behaviour (duplicates) & tests
guenteru 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 |
---|---|---|
|
@@ -1674,3 +1674,44 @@ def test_tuple_correct_keyerror(): | |
[3, 4]])) | ||
with tm.assert_raises_regex(KeyError, "(7, 8)"): | ||
df.groupby((7, 8)).mean() | ||
|
||
|
||
def test_dup_index_names(): | ||
# dup. index names in groupby operations should be renamed (GH 19029): | ||
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 parameterize this |
||
df = pd.DataFrame({'date': pd.date_range('5.1.2018', '5.3.2018'), | ||
'vals': list(range(3))}) | ||
|
||
# duplicates get suffixed by integer position | ||
mi = pd.MultiIndex.from_product([[5], [1, 2, 3]], | ||
names=['date_0', 'date_1']) | ||
expected = pd.Series(data=list(range(3)), index=mi, name='vals') | ||
result = df.groupby([df.date.dt.month, df.date.dt.day])['vals'].sum() | ||
|
||
tm.assert_series_equal(result, expected) | ||
|
||
# 2 out of 3 are duplicates and None | ||
mi = pd.MultiIndex.from_product([[2018], [5], [1, 2, 3]], | ||
names=['0', '1', 'date']) | ||
expected = pd.Series(data=list(range(3)), index=mi, name='vals') | ||
result = df.groupby([df.date.dt.year.rename(None), | ||
df.date.dt.month.rename(None), | ||
df.date.dt.day])['vals'].sum() | ||
tm.assert_series_equal(result, expected) | ||
|
||
# 2 out of 3 names (not None) are duplicates, the remaining is None | ||
mi = pd.MultiIndex.from_product([[2018], [5], [1, 2, 3]], | ||
names=['date_0', None, 'date_1']) | ||
expected = pd.Series(data=list(range(3)), index=mi, name='vals') | ||
result = df.groupby([df.date.dt.year, | ||
df.date.dt.month.rename(None), | ||
df.date.dt.day])['vals'].sum() | ||
tm.assert_series_equal(result, expected) | ||
|
||
# all are None | ||
mi = pd.MultiIndex.from_product([[2018], [5], [1, 2, 3]], | ||
names=[None, None, None]) | ||
expected = pd.Series(data=list(range(3)), index=mi, name='vals') | ||
result = df.groupby([df.date.dt.year.rename(None), | ||
df.date.dt.month.rename(None), | ||
df.date.dt.day.rename(None)])['vals'].sum() | ||
tm.assert_series_equal(result, expected) |
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 |
---|---|---|
|
@@ -1705,9 +1705,21 @@ def test_crosstab_with_numpy_size(self): | |
tm.assert_frame_equal(result, expected) | ||
|
||
def test_crosstab_dup_index_names(self): | ||
# GH 13279, GH 18872 | ||
# duplicated index name should get renamed (GH 19029) | ||
s = pd.Series(range(3), name='foo') | ||
pytest.raises(ValueError, pd.crosstab, s, s) | ||
failed = False | ||
try: | ||
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. if you are asserting that this works, simply collect the result and compare vs expected |
||
result = pd.crosstab(s, s) | ||
except ValueError: | ||
failed = True | ||
|
||
assert failed is False | ||
|
||
s0 = pd.Series(range(3), name='foo0') | ||
s1 = pd.Series(range(3), name='foo1') | ||
expected = pd.DataFrame(np.diag(np.ones(3, dtype='int64')), | ||
index=s0, columns=s1) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
@pytest.mark.parametrize("names", [['a', ('b', 'c')], | ||
[('a', 'b'), 'c']]) | ||
|
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.
this is super complicated, what exactly are you trying to do 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.
Hi. The goal here is to add a suffix to duplicate entries:
Before the current version the code just enumerated all the entries regardless of their multiplicity:
For some reason I thought that the current version would be better suited.
I can switch back to the old (and probably less confusing) version if you want.