Skip to content

BUG: pivot_table with multi-index columns only fails #31013

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

Merged
merged 11 commits into from
Jan 15, 2020
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Reshaping
^^^^^^^^^

-
-
- Bug in :meth:`DataFrame.pivot_table` when only MultiIndexed columns is set (:issue:`17038`)

Sparse
^^^^^^
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ def pivot_table(
agged[v] = maybe_downcast_to_dtype(agged[v], data[v].dtype)

table = agged
if table.index.nlevels > 1:

# GH17038, this check should only happen if index is defined (not None)
if table.index.nlevels > 1 and index:
# Related GH #17123
# If index_names are integers, determine whether the integers refer
# to the level position or name.
Expand Down
20 changes: 14 additions & 6 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,12 +896,6 @@ def _check_output(
totals = table.loc[("All", ""), value_col]
assert totals == self.data[value_col].mean()

# no rows
rtable = self.data.pivot_table(
columns=["AA", "BB"], margins=True, aggfunc=np.mean
)
assert isinstance(rtable, Series)

Comment on lines -899 to -904
Copy link
Member Author

Choose a reason for hiding this comment

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

this gives a wrong shape, should not return Series but a DataFrame with AA and BB for columns, I created an issue to address it!

table = self.data.pivot_table(index=["AA", "BB"], margins=True, aggfunc="mean")
for item in ["DD", "EE", "FF"]:
totals = table.loc[("All", ""), item]
Expand Down Expand Up @@ -951,6 +945,20 @@ def test_margins_dtype_len(self):

tm.assert_frame_equal(expected, result)

@pytest.mark.parametrize("cols", [(1, 2), ("a", "b"), (1, "b"), ("a", 1)])
def test_pivot_table_multiindex_only(self, cols):
# GH 17038
df2 = DataFrame({cols[0]: [1, 2, 3], cols[1]: [1, 2, 3], "v": [4, 5, 6]})

result = df2.pivot_table(values="v", columns=cols)
expected = DataFrame(
[[4, 5, 6]],
columns=MultiIndex.from_tuples([(1, 1), (2, 2), (3, 3)], names=cols),
index=Index(["v"]),
)

tm.assert_frame_equal(result, expected)

def test_pivot_integer_columns(self):
# caused by upstream bug in unstack

Expand Down