python - Pandas pivot_table 发出关于在日期类型的数据集上使用边距时推断 datetime64 的 FutureWarning
问题描述
创建在索引中包含日期字段的数据透视表时出现FutureWarning
错误:
<ipython-input-22-330e19e65ef9>:1: 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={value.dtype})
pivot_report = pd.pivot_table(
这是触发它的代码:
pivot_report = pd.pivot_table(
data.loc[(data["year"] == year_to_report)],
index = ["Category", "Subcategory", "date"],
values = [ "amount"],
aggfunc = [np.sum],
margins = True)
pivot_report
数据是从具有显式类型的 csv 读取的,并且该字段似乎在调用之前date
已正确转换为,所以我不清楚它为什么抱怨类型推断。datetime64
pivot_table
data['date']
0 2021-09-10
1 2021-09-08
2 2021-09-08
3 2021-09-08
4 2021-09-08
...
37299 2008-07-31
37300 2008-07-31
37301 2008-07-31
37302 2008-07-31
37303 2008-07-31
Name: date, Length: 37304, dtype: datetime64[ns]
但是我注意到,虽然该margins
参数All
按预期在底部添加了一行,显示了该amount
字段的总计,但它也显示了NaT
该字段的总计date
。
sum
amount
Category Subcategory date
Auto & Transport Advertising 2021-01-01 0.00
2021-01-02 0.00
2021-01-03 0.00
2021-01-04 0.00
2021-01-05 0.00
... ...
Vacation Withdrawal 2021-09-06 0.00
2021-09-07 0.00
2021-09-08 0.00
2021-09-10 0.00
All NaT 134.31
[928450 rows x 1 columns]
这是预期的吗?为什么margins
要尝试为日期字段提供总计?这会解释类型推断FutureWarning
消息吗?
解决方案
推荐阅读
- cmd - 我无法对程序文件使用 where 命令
- r - 修改一行数据以获得漂亮的输出(摘要),重塑 - 将行拆分为新的多列
- java - 缓冲阅读器抛出异常
- testing - 赛普拉斯,如何检查属性是否存在
- python - Fizzbuzz Challenge in Twilio quest
- html - 如何在 WordPress 中让这些行彼此相邻?
- python - tkinter - 为什么会有像 bbox 这样的东西?
- python - Tensorflow:PartialTensorShape:合并期间不兼容的等级:2 vs. 1
- php - 调用未定义的函数 apcu_add()
- c# - 内联方法以提高性能,但在 C# 中执行速度较慢