我已经将我的系列强制转换为 dtype= 的日期时间列datetime64[ns]
(虽然只需要一天的分辨率......不知道如何改变)。
import pandas as pd
df = pd.read_csv('somefile.csv')
column = df['date']
column = pd.to_datetime(column, coerce=True)
但绘图不起作用:
ipdb> column.plot(kind='hist')
*** TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('float64')
我想绘制一个直方图按周、月或年显示日期计数.
当然有一种方法可以做到这一点pandas
?
给定这个 df:
date
0 2001-08-10
1 2002-08-31
2 2003-08-29
3 2006-06-21
4 2002-03-27
5 2003-07-14
6 2004-06-15
7 2003-08-14
8 2003-07-29
并且,如果情况还不是这样的话:
df["date"] = df["date"].astype("datetime64")
要按月显示日期计数:
df.groupby(df["date"].dt.month).count().plot(kind="bar")
.dt
允许您访问日期时间属性。
这会给你:
您可以逐年、逐日等替换。
例如,如果您想区分年份和月份,只需执行以下操作:
df.groupby([df["date"].dt.year, df["date"].dt.month]).count().plot(kind="bar")
这使:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)