Skip to content

Commit eb29839

Browse files
Backport PR #48713 on branch 1.5.x (BUG: pivot_table raising Future Warning 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 <[email protected]>
1 parent 98b8aa0 commit eb29839

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

doc/source/whatsnew/v1.5.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Fixed regressions
2727
Bug fixes
2828
~~~~~~~~~
2929
- Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`48667`)
30+
- Bug in :meth:`DataFrame.pivot_table` raising unexpected ``FutureWarning`` when setting datetime column as index (:issue:`48683`)
3031
-
3132

3233
.. ---------------------------------------------------------------------------

pandas/core/reshape/pivot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def _add_margins(
318318

319319
from pandas import DataFrame
320320

321-
margin_dummy = DataFrame(row_margin, columns=[key]).T
321+
margin_dummy = DataFrame(row_margin, columns=Index([key])).T
322322

323323
row_names = result.index.names
324324
# check the result column and leave floats

pandas/tests/reshape/test_pivot.py

+32
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,38 @@ def test_pivot_ea_dtype_dropna(self, dropna):
22372237
)
22382238
tm.assert_frame_equal(result, expected)
22392239

2240+
def test_pivot_table_datetime_warning(self):
2241+
# GH#48683
2242+
df = DataFrame(
2243+
{
2244+
"a": "A",
2245+
"b": [1, 2],
2246+
"date": pd.Timestamp("2019-12-31"),
2247+
"sales": [10.0, 11],
2248+
}
2249+
)
2250+
with tm.assert_produces_warning(None):
2251+
result = df.pivot_table(
2252+
index=["b", "date"], columns="a", margins=True, aggfunc="sum"
2253+
)
2254+
expected = DataFrame(
2255+
[[10.0, 10.0], [11.0, 11.0], [21.0, 21.0]],
2256+
index=MultiIndex.from_arrays(
2257+
[
2258+
Index([1, 2, "All"], name="b"),
2259+
Index(
2260+
[pd.Timestamp("2019-12-31"), pd.Timestamp("2019-12-31"), ""],
2261+
dtype=object,
2262+
name="date",
2263+
),
2264+
]
2265+
),
2266+
columns=MultiIndex.from_tuples(
2267+
[("sales", "A"), ("sales", "All")], names=[None, "a"]
2268+
),
2269+
)
2270+
tm.assert_frame_equal(result, expected)
2271+
22402272

22412273
class TestPivot:
22422274
def test_pivot(self):

0 commit comments

Comments
 (0)