Skip to content

Commit a4d6323

Browse files
phoflnoatamir
authored andcommitted
BUG: pivot_table raising Future Warning with datetime column as index (pandas-dev#48713)
1 parent 49fd147 commit a4d6323

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
@@ -26,6 +26,7 @@ Fixed regressions
2626
Bug fixes
2727
~~~~~~~~~
2828
- Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`48667`)
29+
- Bug in :meth:`DataFrame.pivot_table` raising unexpected ``FutureWarning`` when setting datetime column as index (:issue:`48683`)
2930
-
3031

3132
.. ---------------------------------------------------------------------------

pandas/core/reshape/pivot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def _add_margins(
320320

321321
from pandas import DataFrame
322322

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

325325
row_names = result.index.names
326326
# check the result column and leave floats

pandas/tests/reshape/test_pivot.py

+32
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,38 @@ def test_pivot_ea_dtype_dropna(self, dropna):
22542254
)
22552255
tm.assert_frame_equal(result, expected)
22562256

2257+
def test_pivot_table_datetime_warning(self):
2258+
# GH#48683
2259+
df = DataFrame(
2260+
{
2261+
"a": "A",
2262+
"b": [1, 2],
2263+
"date": pd.Timestamp("2019-12-31"),
2264+
"sales": [10.0, 11],
2265+
}
2266+
)
2267+
with tm.assert_produces_warning(None):
2268+
result = df.pivot_table(
2269+
index=["b", "date"], columns="a", margins=True, aggfunc="sum"
2270+
)
2271+
expected = DataFrame(
2272+
[[10.0, 10.0], [11.0, 11.0], [21.0, 21.0]],
2273+
index=MultiIndex.from_arrays(
2274+
[
2275+
Index([1, 2, "All"], name="b"),
2276+
Index(
2277+
[pd.Timestamp("2019-12-31"), pd.Timestamp("2019-12-31"), ""],
2278+
dtype=object,
2279+
name="date",
2280+
),
2281+
]
2282+
),
2283+
columns=MultiIndex.from_tuples(
2284+
[("sales", "A"), ("sales", "All")], names=[None, "a"]
2285+
),
2286+
)
2287+
tm.assert_frame_equal(result, expected)
2288+
22572289

22582290
class TestPivot:
22592291
def test_pivot(self):

0 commit comments

Comments
 (0)