我使用datetime.date字段作为我的索引,但由于某些原因,它似乎不同意它是datetime。
首先,我将验证字段是否确实是datetime.date格式:
In [1]: df[date][0]
Out [1]: datetime.date(2010, 1, 1)现在我尝试按周重新采样,并绘制平均值:
In [2]: df.set_index(date)[var].resample('W', how='mean').plot()
/python2.7/site-packages/pandas/core/generic.pyc in resample(self, rule, how, axis, fill_method, closed, label, convention, kind, loffset, limit, base)
2878 fill_method=fill_method, convention=convention,
2879 limit=limit, base=base)
-> 2880 return sampler.resample(self).__finalize__(self)
2881
2882 def first(self, offset):
/python2.7/site-packages/pandas/tseries/resample.pyc in resample(self, obj)
100 return self.obj
101 else: # pragma: no cover
--> 102 raise TypeError('Only valid with DatetimeIndex or PeriodIndex')
103
104 rs_axis = rs._get_axis(self.axis)
TypeError: Only valid with DatetimeIndex or PeriodIndex我不能提供样本数据,因为它是专有的。对这里可能发生的事情有什么想法吗?
发布于 2015-05-22 23:35:48
我认为您应该首先将date列转换为pandas时间序列。尝试:
df['date'] = pd.to_datetime(df.date)
df.set_index('date')[var]....Pandas dev告诉我,pandas索引中的datetime对象是低效的,应该尽可能避免。
https://stackoverflow.com/questions/30399528
复制相似问题