【问题标题】:Pandas resample function not working on DateTimeIndexPandas 重新采样功能不适用于 DateTimeIndex
【发布时间】:2015-09-15 05:15:49
【问题描述】:

我有一个名为 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 |
+--------------+-------------+

我将索引设为 DateTimeIndex,如下所示: aus.index = pd.to_datetime(aus.index)

然后我运行这样的检查:type(aus.index),给定的输出是pandas.tseries.index.DatetimeIndex

然后当我尝试像这样将索引重新采样到几周时 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

我之前的类型检查表明我有正确的索引,但 resample 函数不这么认为。 有什么想法吗?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    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
    

    使用轴=0:

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

    注意:这是重采样的默认值:

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

    【讨论】:

    • @meter 你能粘贴bin/something 一个可重现的例子吗?我不知道什么会让你不经意间。
    • 解决了这个问题。对我来说有些愚蠢。感谢您的回答,我挠了一个小时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    相关资源
    最近更新 更多