首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Resample不能将我的索引识别为datetime

Resample不能将我的索引识别为datetime
EN

Stack Overflow用户
提问于 2015-05-22 22:36:25
回答 1查看 214关注 0票数 0

我使用datetime.date字段作为我的索引,但由于某些原因,它似乎不同意它是datetime。

首先,我将验证字段是否确实是datetime.date格式:

代码语言:javascript
复制
In [1]: df[date][0]

Out [1]: datetime.date(2010, 1, 1)

现在我尝试按周重新采样,并绘制平均值:

代码语言:javascript
复制
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

我不能提供样本数据,因为它是专有的。对这里可能发生的事情有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2015-05-22 23:35:48

我认为您应该首先将date列转换为pandas时间序列。尝试:

代码语言:javascript
复制
df['date'] = pd.to_datetime(df.date)
df.set_index('date')[var]....

Pandas dev告诉我,pandas索引中的datetime对象是低效的,应该尽可能避免。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30399528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档