Skip to content

BUG: pivot_table over Categorical Columns #15511

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

Closed
wants to merge 4 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,4 @@ Bug Fixes
- Bug in ``pd.melt()`` where passing a tuple value for ``value_vars`` caused a ``TypeError`` (:issue:`15348`)
- Bug in ``.eval()`` which caused multiline evals to fail with local variables not on the first line (:issue:`15342`)
- Bug in ``pd.read_msgpack`` which did not allow to load dataframe with an index of type ``CategoricalIndex`` (:issue:`15487`)
- Bug in ``DataFrame.pivot_table`` where ``dropna=True`` would not drop columns from a categorical series to columns (:issue:`15193`)
33 changes: 33 additions & 0 deletions pandas/tests/tools/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,39 @@ def test_pivot_table_dropna(self):
tm.assert_index_equal(pv_col.columns, m)
tm.assert_index_equal(pv_ind.index, m)

def test_pivot_table_dropna_categoricals(self):
# GH 15193
categories = ['a', 'b', 'c', 'd']

df = DataFrame({'A': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'],
'B': [1, 2, 3, 1, 2, 3, 1, 2, 3],
'C': range(0, 9)})

df['A'] = df['A'].astype('category', ordered=False,
categories=categories)
result_true = df.pivot_table(index='B', columns='A', values='C',
dropna=True)
expected_columns = Series(['a', 'b', 'c'], name='A')
expected_columns = expected_columns.astype('category', ordered=False,
categories=categories)
expected_index = Series([1, 2, 3], name='B')
expected_true = DataFrame([[0.0, 3.0, 6.0],
[1.0, 4.0, 7.0],
[2.0, 5.0, 8.0]],
index=expected_index,
columns=expected_columns,)
tm.assert_frame_equal(expected_true, result_true)

result_false = df.pivot_table(index='B', columns='A', values='C',
dropna=False)
expected_columns = Series(['a', 'b', 'c', 'd'], name='A')
expected_false = DataFrame([[0.0, 3.0, 6.0, np.NaN],
[1.0, 4.0, 7.0, np.NaN],
[2.0, 5.0, 8.0, np.NaN]],
index=expected_index,
columns=expected_columns,)
tm.assert_frame_equal(expected_false, result_false)

def test_pass_array(self):
result = self.data.pivot_table(
'D', index=self.data.A, columns=self.data.C)
Expand Down
4 changes: 4 additions & 0 deletions pandas/tools/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',
if len(index) == 0 and len(columns) > 0:
table = table.T

# GH 15193 Makse sure empty columns are removed if dropna=True
if isinstance(table, DataFrame) and dropna:
table = table.dropna(how='all', axis=1)

return table


Expand Down