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 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.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -540,3 +540,4 @@ of columns didn't match the number of series provided (:issue:`12039`).

- Bug in ``.loc`` setitem indexer preventing the use of a TZ-aware DatetimeIndex (:issue:`12050`)
- Big in ``.style`` indexes and multi-indexes not appearing (:issue:`11655`)
Copy link
Contributor

Choose a reason for hiding this comment

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

move to 0.18.1

- Bug in ``.pivot_table()`` where ``dropna`` argument drops columns and index level names (:issue:`12133`)
6 changes: 4 additions & 2 deletions pandas/tools/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ 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
12 changes: 12 additions & 0 deletions pandas/tools/tests/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ def test_pivot_table_dropna(self):
assert_equal(pv_col.columns.values, m.values)
assert_equal(pv_ind.index.values, m.values)

#issue 12133, dropna=False arg drops index level names
idx = pd.MultiIndex.from_tuples([(1000000, 201308), (1000000, 201310)],
Copy link
Contributor

Choose a reason for hiding this comment

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

add the issue number as a comment and a line explaining what it is

names=('quantity', 'month'))
tup = (("B", "c"), ("B", "d"), ("C", "c"), ("C", "d"))
clm = pd.MultiIndex.from_tuples(tup, names=('customer', 'product'))
pv = pd.DataFrame([[50000, np.nan, np.nan, np.nan],
[np.nan, np.nan, np.nan, 30000]],
index=idx, columns=clm)
pv_gen = df[2:].pivot_table('amount', ['quantity', 'month'],
['customer', 'product'], dropna=False)
tm.assert_frame_equal(pv, pv_gen)

def test_pass_array(self):
result = self.data.pivot_table(
'D', index=self.data.A, columns=self.data.C)
Expand Down