Skip to content

BUG: Fix for GH #14848 for groupby().describe() with tuples as the Index #15110

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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ Bug Fixes
- Bug in converting object elements of array-like objects to unsigned 64-bit integers (:issue:`4471`, :issue:`14982`)
- Bug in ``pd.pivot_table()`` where no error was raised when values argument was not in the columns (:issue:`14938`)

- Bug in ``DataFrame.groupby().describe()`` when grouping on ``Index`` containing tuples (:issue:`14848`)
- Raise `ValueError` if creating a `MultiIndex` with tuples and not passing a list of names (:issue:`15110`)



Expand Down
4 changes: 4 additions & 0 deletions pandas/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ def _set_names(self, names, level=None, validate=True):
that it only acts on copies
"""

# GH 15110
# Don't allow a single string for names in a MultiIndex
if names is not None and not is_list_like(names):
raise ValueError('Names should be list-like for a MultiIndex')
names = list(names)

if validate and level is not None and len(names) != len(level):
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,19 @@ def test_frame_describe_multikey(self):
for name, group in groupedT:
assert_frame_equal(result[name], group.describe())

def test_frame_describe_tupleindex(self):

# GH 14848 - regression from 0.19.0 to 0.19.1
df1 = DataFrame({'x': [1, 2, 3, 4, 5] * 3,
Copy link
Contributor

Choose a reason for hiding this comment

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

comment inside the test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK

'y': [10, 20, 30, 40, 50] * 3,
'z': [100, 200, 300, 400, 500] * 3})
df1['k'] = [(0, 0, 1), (0, 1, 0), (1, 0, 0)] * 5
df2 = df1.rename(columns={'k': 'key'})
result = df1.groupby('k').describe()
expected = df2.groupby('key').describe()
expected.index.set_names(result.index.names, inplace=True)
assert_frame_equal(result, expected)

def test_frame_groupby(self):
grouped = self.tsframe.groupby(lambda x: x.weekday())

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2554,3 +2554,12 @@ def test_unsortedindex(self):

with assertRaises(KeyError):
df.loc(axis=0)['q', :]

def test_tuples_with_name_string(self):
# GH 15110 and GH 14848

li = [(0, 0, 1), (0, 1, 0), (1, 0, 0)]
with assertRaises(ValueError):
pd.Index(li, name='abc')
with assertRaises(ValueError):
pd.Index(li, name='a')