Skip to content

Commit ab0d236

Browse files
Dr-Irvjreback
authored andcommitted
BUG: Fix for GH #14848 for groupby().describe() with tuples as the Index
closes #14848 Author: Dr-Irv <[email protected]> Closes #15110 from Dr-Irv/Issue14848 and squashes the following commits: c18c6cb [Dr-Irv] Undo change to merge.py and make whatsnew a 2 line comment. db13c3b [Dr-Irv] Use not is_list_like fbd20f5 [Dr-Irv] Raise error when creating index of tuples with name parameter a string f3a7a21 [Dr-Irv] Changes per jreback requests 9489cb2 [Dr-Irv] BUG: Fix issue #14848 groupby().describe() on indices containing all tuples
1 parent c9e7aee commit ab0d236

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

doc/source/whatsnew/v0.20.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ Bug Fixes
352352
- Bug in converting object elements of array-like objects to unsigned 64-bit integers (:issue:`4471`, :issue:`14982`)
353353
- Bug in ``pd.pivot_table()`` where no error was raised when values argument was not in the columns (:issue:`14938`)
354354

355+
- Bug in ``DataFrame.groupby().describe()`` when grouping on ``Index`` containing tuples (:issue:`14848`)
356+
- Bug in creating a ``MultiIndex`` with tuples and not passing a list of names; this will now raise ``ValueError`` (:issue:`15110`)
355357

356358

357359

pandas/indexes/multi.py

+4
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,10 @@ def _set_names(self, names, level=None, validate=True):
490490
that it only acts on copies
491491
"""
492492

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

495499
if validate and level is not None and len(names) != len(level):

pandas/tests/groupby/test_groupby.py

+13
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,19 @@ def test_frame_describe_multikey(self):
14901490
for name, group in groupedT:
14911491
assert_frame_equal(result[name], group.describe())
14921492

1493+
def test_frame_describe_tupleindex(self):
1494+
1495+
# GH 14848 - regression from 0.19.0 to 0.19.1
1496+
df1 = DataFrame({'x': [1, 2, 3, 4, 5] * 3,
1497+
'y': [10, 20, 30, 40, 50] * 3,
1498+
'z': [100, 200, 300, 400, 500] * 3})
1499+
df1['k'] = [(0, 0, 1), (0, 1, 0), (1, 0, 0)] * 5
1500+
df2 = df1.rename(columns={'k': 'key'})
1501+
result = df1.groupby('k').describe()
1502+
expected = df2.groupby('key').describe()
1503+
expected.index.set_names(result.index.names, inplace=True)
1504+
assert_frame_equal(result, expected)
1505+
14931506
def test_frame_groupby(self):
14941507
grouped = self.tsframe.groupby(lambda x: x.weekday())
14951508

pandas/tests/indexes/test_multi.py

+9
Original file line numberDiff line numberDiff line change
@@ -2554,3 +2554,12 @@ def test_unsortedindex(self):
25542554

25552555
with assertRaises(KeyError):
25562556
df.loc(axis=0)['q', :]
2557+
2558+
def test_tuples_with_name_string(self):
2559+
# GH 15110 and GH 14848
2560+
2561+
li = [(0, 0, 1), (0, 1, 0), (1, 0, 0)]
2562+
with assertRaises(ValueError):
2563+
pd.Index(li, name='abc')
2564+
with assertRaises(ValueError):
2565+
pd.Index(li, name='a')

0 commit comments

Comments
 (0)