Skip to content

FIX: pivot_table dropna=False drops index level names #12327

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 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions pandas/tools/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',

if not dropna:
try:
m = MultiIndex.from_arrays(cartesian_product(table.index.levels))
m = MultiIndex.from_arrays(cartesian_product(table.index.levels), names=table.index.names)
table = table.reindex_axis(m, axis=0)
except AttributeError:
pass # it's a single level

try:
m = MultiIndex.from_arrays(cartesian_product(table.columns.levels))
m = MultiIndex.from_arrays(cartesian_product(table.columns.levels), names=table.columns.names)
table = table.reindex_axis(m, axis=1)
except AttributeError:
pass # it's a single level or a series
Expand Down
15 changes: 9 additions & 6 deletions pandas/tools/tests/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ def test_pivot_table_dropna(self):
'product': {0: 'a', 1: 'b', 2: 'c', 3: 'd'},
'quantity': {0: 2000000, 1: 500000,
2: 1000000, 3: 1000000}})
pv_col = df.pivot_table('quantity', 'month', [
'customer', 'product'], dropna=False)
pv_ind = df.pivot_table(
'quantity', ['customer', 'product'], 'month', dropna=False)

names1, names2 = ['month'], ['customer', 'product']
pv_col = df.pivot_table('quantity', names1, names2, dropna=False)
pv_ind = df.pivot_table('quantity', names2, names1, dropna=False)

m = MultiIndex.from_tuples([(u('A'), u('a')),
(u('A'), u('b')),
(u('A'), u('c')),
Expand All @@ -92,7 +91,11 @@ def test_pivot_table_dropna(self):
(u('C'), u('b')),
(u('C'), u('c')),
(u('C'), u('d'))])


assert_equal(pv_col.index.names, names1)
assert_equal(pv_ind.columns.names, names1)
Copy link
Contributor

Choose a reason for hiding this comment

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

construct an expected frame, and use assert_frame_equal

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jreback do you mean i need to fully rewrite test_pivot_table_dropna or just make a separate DataFrame for this case?

assert_equal(pv_col.columns.names, names2)
assert_equal(pv_ind.index.names, names2)
assert_equal(pv_col.columns.values, m.values)
assert_equal(pv_ind.index.values, m.values)

Expand Down