Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[ ] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
print(pd.__version__)
# 1.5.3
df = pd.DataFrame({'A': [1, 1, 2], 'B': [4, 5, 6], 'date': ['2018-01-01', '2018-01-02', '2018-01-03'], "category": ["a", "b", "c"]})
df["date"] = pd.to_datetime(df["date"])
print(df.dtypes)
# A int64
# B int64
# date datetime64[ns]
# category object
# dtype: object
pt = df.pivot_table(index=["date", "category"], columns=["A"], values=["B"], margins=True)
print(pt.index.dtypes)
# date object
# category object
Issue Description
When calling pivot_table
with margins=True
, the dtype of date columns changes from datetime64
to object
. This does not happen when margins=False
.
Expected Behavior
Dtypes should be preserved.
Installed Versions
Comment From: wesleycheung0
Should this be the expected behavior instead, because of the added All
row when margins=True
?
Comment From: KarlisKV
@diego-pm I managed to get the correct type with margins True, got this warning together with it
pt = df.pivot_table(index=["A", "date"], columns=["A"], values=["B"], margins=True) print(pt.index.dtypes)
A object date datetime64[ns] dtype: object
FutureWarning: Inferring datetime64[ns] from data containing strings is deprecated and will be removed in a future version. To retain the old behavior explicitly pass Series(data, dtype=datetime64[ns])
Comment From: diego-pm
@KarlisKV It seems in your case works because the date is not in the first position of the index. The reason must be what @wesleycheung0 said.
Comment From: DeaMariaLeon
@diego-pm I think this is normal, since there is a mix of data types in the index. Maybe this helps:
>>> index= [df["date"][0], "my_string", True]
>>> y = pd.Series(data=(1, 2, 3), index=index)
>>> y.index
Index([2018-01-01 00:00:00, 'my_string', True], dtype='object')
Thank you for opening the issue. I will close it for now. If you feel that it should stay open please let me know.