Skip to content

Commit 74cac0e

Browse files
committed
TST: add test case from Issue pandas-dev#10989
1 parent 2b04d9f commit 74cac0e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

pandas/tools/pivot.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ def convert_categorical(ind):
170170
return _convert(ind)
171171

172172
table.index = convert_categorical(table.index)
173-
table.columns = convert_categorical(table.columns)
173+
if hasattr(table, 'columns'):
174+
table.columns = convert_categorical(table.columns)
174175

175176
if not values and isinstance(table, Series):
176177
# If there are no values and the table is a series, then there is only

pandas/tools/tests/test_pivot.py

+14
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,20 @@ def test_crosstab_dropna(self):
719719
('two', 'dull'), ('two', 'shiny')])
720720
assert_equal(res.columns.values, m.values)
721721

722+
def test_categorical_margins(self):
723+
# GH 10989
724+
data = pd.DataFrame({'x': np.arange(8),
725+
'y': np.arange(8) // 4,
726+
'z': np.arange(8) % 2})
727+
data.y = data.y.astype('category')
728+
data.z = data.z.astype('category')
729+
table = data.pivot_table('x', 'y', 'z', margins=True)
730+
assert_equal(table.values, [[1, 2, 1.5],
731+
[5, 6, 5.5],
732+
[3, 4, 3.5]])
733+
734+
735+
722736
if __name__ == '__main__':
723737
import nose
724738
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

0 commit comments

Comments
 (0)