【发布时间】:2018-06-22 19:34:03
【问题描述】:
我有这样的数据。
Date_of_event - expenditure
2016-04-04 09:20:04.903 - 50000
2015-04-04 09:20:04.903 - 40000
2016-08-04 09:20:04.903 - 70000
2017-01-04 09:20:04.903 - 35000
2011-04-04 09:20:04.903 - 250000
2013-04-06 09:20:04.903 - 500
2016-07-09 09:20:04.903 - 1090000
我想使用 seaborn 或 matplotlib 绘制平均每月支出的六个月移动平均值。作为第一步,我尝试使用重采样计算平均每月支出,然后我按月分组。但是,当我使用此代码时出现以下错误:
df['Date_of_event'] = df['Date_time'].resample('M')
Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'.
"。请注意 .dt.days 和 .dt.month 方法在我的 df['Date_of_event'] 上有效,因此它似乎是一个日期时间对象。
有没有办法将这些日期时间重新采样为年+月,或者更简单的方法来计算移动平均值?
【问题讨论】:
-
您是否希望移动窗口在一个月内,这样您每个月就会得到多个值?或者您想获得每月总计,然后在移动窗口内平均?
标签: pandas datetime matplotlib apply seaborn