Skip to content

Commit 2b04d9f

Browse files
committed
BUG: quick fix for pandas-dev#10989
1 parent 91b6848 commit 2b04d9f

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

pandas/tools/pivot.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ def _add_margins(table, data, values, rows, cols, aggfunc):
159159

160160
grand_margin = _compute_grand_margin(data, values, aggfunc)
161161

162+
# categorical index or columns will fail below when 'All' is added
163+
# here we'll convert all categorical indices to object
164+
def convert_categorical(ind):
165+
_convert = lambda ind: (ind.astype('object')
166+
if ind.dtype.name == 'category' else ind)
167+
if isinstance(ind, MultiIndex):
168+
return ind.set_levels([_convert(lev) for lev in ind.levels])
169+
else:
170+
return _convert(ind)
171+
172+
table.index = convert_categorical(table.index)
173+
table.columns = convert_categorical(table.columns)
174+
162175
if not values and isinstance(table, Series):
163176
# If there are no values and the table is a series, then there is only
164177
# one column in the data. Compute grand margin and return it.

0 commit comments

Comments
 (0)