Skip to content

Commit fed9801

Browse files
committed
BUG: Fix issue pandas-dev#14848 groupby().describe() on indices containing all tuples
1 parent 41170d4 commit fed9801

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

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

353+
- Bug in ``DataFrame.groupby().describe()`` when grouping on an ``Index`` containing tuples (:issue:`14848`)
353354

354355

355356

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+
# GH #14848
1494+
def test_frame_describe_tupleindex(self):
1495+
df1 = DataFrame({'x': [1, 2, 3, 4, 5] * 3,
1496+
'y': [10, 20, 30, 40, 50] * 3,
1497+
'z': [100, 200, 300, 400, 500] * 3})
1498+
df1['k'] = [(0, 0, 1), (0, 1, 0), (1, 0, 0)] * 5
1499+
df2 = df1.rename(columns={'k': 'key'})
1500+
des1 = df1.groupby('k').describe()
1501+
des2 = df2.groupby('key').describe()
1502+
if len(des1) > 0:
1503+
des2.index.set_names(des1.index.names, inplace=True)
1504+
assert_frame_equal(des1, des2)
1505+
14931506
def test_frame_groupby(self):
14941507
grouped = self.tsframe.groupby(lambda x: x.weekday())
14951508

pandas/tools/merge.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,14 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None,
16261626
clean_objs.append(v)
16271627
objs = clean_objs
16281628
name = getattr(keys, 'name', None)
1629-
keys = Index(clean_keys, name=name)
1629+
# GH 14848
1630+
# If coming from groupby(), keys and levels are specified
1631+
# as Index type, so don't tupleize
1632+
tupleize_cols = (levels is None or
1633+
(isinstance(levels, list) and
1634+
not isinstance(keys, Index) and
1635+
not isinstance(levels[0], Index)))
1636+
keys = Index(clean_keys, name=name, tupleize_cols=tupleize_cols)
16301637

16311638
if len(objs) == 0:
16321639
raise ValueError('All objects passed were None')

0 commit comments

Comments
 (0)