Skip to content
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

BUG: pivot_table with margins=True fails for categorical dtype, #10989 #10993

Closed
wants to merge 2 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
14 changes: 14 additions & 0 deletions pandas/tools/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ def _add_margins(table, data, values, rows, cols, aggfunc):

grand_margin = _compute_grand_margin(data, values, aggfunc)

# categorical index or columns will fail below when 'All' is added
# here we'll convert all categorical indices to object
def convert_categorical(ind):
_convert = lambda ind: (ind.astype('object')
if ind.dtype.name == 'category' else ind)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not quite right, this should end of being a cat level.

can you add the tests case and i'll take a look. thx.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test case is in the linked issue. Or would you like me to add a unit test to the package as part of this PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes pls

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

Choose a reason for hiding this comment

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

this is too specific
need to be more general and/or pushed down into the index itself

if isinstance(ind, MultiIndex):
return ind.set_levels([_convert(lev) for lev in ind.levels])
else:
return _convert(ind)

table.index = convert_categorical(table.index)
if hasattr(table, 'columns'):
table.columns = convert_categorical(table.columns)

if not values and isinstance(table, Series):
# If there are no values and the table is a series, then there is only
# one column in the data. Compute grand margin and return it.
Expand Down
14 changes: 14 additions & 0 deletions pandas/tools/tests/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,20 @@ def test_crosstab_dropna(self):
('two', 'dull'), ('two', 'shiny')])
assert_equal(res.columns.values, m.values)

def test_categorical_margins(self):
# GH 10989
data = pd.DataFrame({'x': np.arange(8),
'y': np.arange(8) // 4,
'z': np.arange(8) % 2})
data.y = data.y.astype('category')
data.z = data.z.astype('category')
table = data.pivot_table('x', 'y', 'z', margins=True)
assert_equal(table.values, [[1, 2, 1.5],
[5, 6, 5.5],
[3, 4, 3.5]])



if __name__ == '__main__':
import nose
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down