Skip to content

Commit

Permalink
Backport PR #48713 on branch 1.5.x (BUG: pivot_table raising Future W…
Browse files Browse the repository at this point in the history
…arning with datetime column as index) (#48742)

Backport PR #48713: BUG: pivot_table raising Future Warning with datetime column as index

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl committed Sep 23, 2022
1 parent 98b8aa0 commit eb29839
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Fixed regressions
Bug fixes
~~~~~~~~~
- Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`48667`)
- Bug in :meth:`DataFrame.pivot_table` raising unexpected ``FutureWarning`` when setting datetime column as index (:issue:`48683`)
-

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def _add_margins(

from pandas import DataFrame

margin_dummy = DataFrame(row_margin, columns=[key]).T
margin_dummy = DataFrame(row_margin, columns=Index([key])).T

row_names = result.index.names
# check the result column and leave floats
Expand Down
32 changes: 32 additions & 0 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,38 @@ def test_pivot_ea_dtype_dropna(self, dropna):
)
tm.assert_frame_equal(result, expected)

def test_pivot_table_datetime_warning(self):
# GH#48683
df = DataFrame(
{
"a": "A",
"b": [1, 2],
"date": pd.Timestamp("2019-12-31"),
"sales": [10.0, 11],
}
)
with tm.assert_produces_warning(None):
result = df.pivot_table(
index=["b", "date"], columns="a", margins=True, aggfunc="sum"
)
expected = DataFrame(
[[10.0, 10.0], [11.0, 11.0], [21.0, 21.0]],
index=MultiIndex.from_arrays(
[
Index([1, 2, "All"], name="b"),
Index(
[pd.Timestamp("2019-12-31"), pd.Timestamp("2019-12-31"), ""],
dtype=object,
name="date",
),
]
),
columns=MultiIndex.from_tuples(
[("sales", "A"), ("sales", "All")], names=[None, "a"]
),
)
tm.assert_frame_equal(result, expected)


class TestPivot:
def test_pivot(self):
Expand Down

0 comments on commit eb29839

Please sign in to comment.