8000 Bug: groupby multiindex levels equals rows by P-Tillmann · Pull Request #16859 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

Bug: groupby multiindex levels equals rows #16859

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 11 commits into from
Aug 24, 2017
Next Next commit
BUG: GH16843 fix groupby
Fix for GH16843 where groubpy gives wrong result when number of
groupedby index+columns is equal to length of axis
  • Loading branch information
P-Tillmann committed Jul 7, 2017
commit 79289601c44b697a0f7b1a0934fdfaace5089799
9 changes: 5 additions & 4 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2628,13 +2628,14 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True,

try:
if isinstance(obj, DataFrame):
all_in_columns = all(g in obj.columns for g in keys)
all_in_columns_index = all(g in obj.columns or g in obj.index.names
for g in keys)
else:
all_in_columns = False
all_in_columns_index = False
except Exception:
all_in_columns = False
all_in_columns_index = False

if not any_callable and not all_in_columns and \
if not any_callable and not all_in_columns_index and \
not any_arraylike and not any_groupers and \
match_axis_length and level is None:
keys = [com._asarray_tuplesafe(keys)]
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3890,6 +3890,17 @@ def predictions(tool):
expected = df1.groupby('Key').apply(predictions).p1
result = df2.groupby('Key').apply(predictions).p1
tm.assert_series_equal(expected, result)

def test_gb_key_len_equal_axis_len(self):
# GH16843
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a 1-liner about what this is testing

df = pd.DataFrame([['foo','bar', 'B', 1],
['foo','bar', 'B', 2],
['foo','baz', 'C', 3]],
columns=['first','second','third','one'])
df.set_index(['first','second',], inplace=True)
df = df.groupby(['first','second','third']).size()
assert df.loc[('foo','bar','B')]==2
assert df.loc[('foo','baz','C')]==1


def _check_groupby(df, result, keys, field, f=lambda x: x.sum()):
Expand Down
0