pandas 重新采样无法正常工作 [英] Pandas resample does not work properly

查看:48
本文介绍了 pandas 重新采样无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从熊猫那里得到了一个奇怪的行为,我想将我的分钟数据重新采样为每小时数据(使用平均值).我的数据如下所示:

I am getting a weird behaviour from pandas, I want to resample my minute data to hourly data (using mean). My data looks as follows:

Data.head()
                      AAA    BBB
Time                              
2009-02-10 09:31:00  86.34  101.00
2009-02-10 09:36:00  86.57  100.50
2009-02-10 09:38:00  86.58   99.78
2009-02-10 09:40:00  86.63   99.75
2009-02-10 09:41:00  86.52   99.66

Data.info()

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 961276 entries, 2009-02-10 09:31:00 to 2016-02-29 19:59:00
Data columns (total 2 columns):
AAA    961276 non-null float64
BBB    961276 non-null float64
dtypes: float64(2)
memory usage: 22.0 MB

Data.index

Out[25]: 
DatetimeIndex(['2009-02-10 09:31:00', '2009-02-10 09:36:00',
               '2009-02-10 09:38:00', '2009-02-10 09:40:00',
               '2009-02-10 09:41:00', '2009-02-10 09:44:00',
               '2009-02-10 09:45:00', '2009-02-10 09:46:00',
               '2009-02-10 09:47:00', '2009-02-10 09:48:00',
               ...
               '2016-02-29 19:41:00', '2016-02-29 19:42:00',
               '2016-02-29 19:43:00', '2016-02-29 19:50:00',
               '2016-02-29 19:52:00', '2016-02-29 19:53:00',
               '2016-02-29 19:56:00', '2016-02-29 19:57:00',
               '2016-02-29 19:58:00', '2016-02-29 19:59:00'],
              dtype='datetime64[ns]', name='Time', length=961276, freq=None)

要重新采样数据,我执行以下操作:

To resample the data I do the following:

tframe = '60T'
hr_mean = Data.resample(tframe).mean()

作为输出,我得到只有两个数字的熊猫系列:

And as the output I get pandas Series with only two numbers in it:

In[26]: hr_mean
Out[26]: 
AAA    156.535198
BBB     30.197029
dtype: float64

如果我选择不同的时间范围或重采样函数,我会得到相同的行为.

I get the same behaviour if I choose different timeframe or resampling function.

推荐答案

您显示的行为是旧版 Pandas (pandas <0.18) 的预期行为.较新的 Pandas 版本具有更改的重采样 API,您可以在此处看到其中一种棘手的情况.

The behaviour you show is the expected behaviour for older pandas versions (pandas < 0.18). Newer pandas versions have a changed resample API, of which you see here one of the tricky cases.

在 v0.18 之前,resample 使用了 how 关键字来指定如何重采样,直接返回一个重采样的帧/序列:

Before v0.18, resample used the how keyword to specify how to resample, and returned a resampled frame/series directly:

In [5]: data = pd.DataFrame(np.random.randn(180, 2), columns=['AAA', 'BBB'], index=pd.date_range("2016-06-01", periods=180, freq='1T'))

# how='mean' is the default, so this is the same as data.resample('60T')
In [6]: data.resample('60T', how='mean')  
Out[6]:
                          AAA       BBB
2016-06-01 00:00:00  0.100026  0.210722
2016-06-01 01:00:00  0.093662 -0.078066
2016-06-01 02:00:00 -0.114801  0.002615

# calling .mean() now calculates the mean of each column, resulting in the following series:
In [7]: data.resample('60T', how='mean').mean()
Out[7]:
AAA    0.026296
BBB    0.045090
dtype: float64

In [8]: pd.__version__
Out[8]: u'0.17.1'

从 0.18.0 开始,resample 本身就是一个延迟操作,这意味着你首先必须调用一个方法(在这种情况下为 mean())来执行实际重采样:

Starting from 0.18.0, resample itself is a deferred operation, meaning that you first have to call a method (in this case mean()) to perform the actual resampling:

In [4]: data.resample('60T')
Out[4]: DatetimeIndexResampler [freq=<60 * Minutes>, axis=0, closed=left, label=left, convention=start, base=0]

In [5]: data.resample('60T').mean()
Out[5]:
                          AAA       BBB
2016-06-01 00:00:00 -0.059038  0.102275
2016-06-01 01:00:00 -0.141429 -0.021342
2016-06-01 02:00:00 -0.073341 -0.150091

In [6]: data.resample('60T').mean().mean()
Out[6]:
AAA   -0.091270
BBB   -0.023052
dtype: float64

In [7]: pd.__version__
Out[7]: '0.18.1'

参见 http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#resample-api 用于解释 API 的变化.

See http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#resample-api for the explanation of the change in API.

这篇关于 pandas 重新采样无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆