简体   繁体   English

Pandas resample函数不能在DateTimeIndex上工作

[英]Pandas resample function not working on DateTimeIndex

I have a data frame called aus that looks like this: 我有一个名为aus的数据框,如下所示:

+--------------+-------------+
|              | link clicks |
+--------------+-------------+
| created_time |             |
| 2015-07-20   |        8600 |
| 2015-07-21   |       11567 |
| 2015-07-22   |        1809 |
| 2015-07-23   |        7032 |
| 2015-07-26   |       23704 |
+--------------+-------------+

I make the index a DateTimeIndex like so: aus.index = pd.to_datetime(aus.index) 我将索引aus.index = pd.to_datetime(aus.index) DateTimeIndex,如下所示: aus.index = pd.to_datetime(aus.index)

Then I run a check like this: type(aus.index) and the given output is pandas.tseries.index.DatetimeIndex 然后我运行这样的检查: type(aus.index) ,给定的输出是pandas.tseries.index.DatetimeIndex

Then when I try to resample the index into weeks like so aus.index = aus.resample('w', how='sum', axis=1) I am met with the following error: 然后,当我尝试将索引重新采样为几周时,如aus.index = aus.resample('w', how='sum', axis=1)我遇到以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-310-3268a3f46a19> in <module>()
----> 1 aus.index = aus.resample('w', how='sum', axis=1)

/usr/local/lib/python2.7/site-packages/pandas/core/generic.pyc in resample(self, rule, how, axis, fill_method, closed, label, convention, kind, loffset, limit, base)
   3264                               fill_method=fill_method, convention=convention,
   3265                               limit=limit, base=base)
-> 3266         return sampler.resample(self).__finalize__(self)
   3267 
   3268     def first(self, offset):

/usr/local/lib/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, TimedeltaIndex or PeriodIndex')
    103 
    104         rs_axis = rs._get_axis(self.axis)

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex

My type check earlier says I have the proper index, but the resample function doesn't thinks so. 我之前的类型检查说我有正确的索引,但resample函数不这么认为。 Any thoughts? 有什么想法吗?

axis=1 means it's trying to resample the columns (which isn't a DatetimeIndex). axis = 1表示它正在尝试重新采样列(不是DatetimeIndex)。

In [11]: df.columns
Out[11]: Index([u'link clicks'], dtype='object')

In [12]: type(df.columns)
Out[12]: pandas.core.index.Index

Use axis=0: 使用axis = 0:

In [21]: aus.resample('w', how='sum', axis=0)
Out[21]:
              link clicks
created_time
2015-07-26          52712

Note: this is the default for resample: 注意:这是resample的默认值:

In [22]: aus.resample('w', how='sum')
Out[22]:
              link clicks
created_time
2015-07-26          52712

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM