pandas 重新采样时间序列向后计数(或反向重新采样) [英] Pandas resample time series counting backwards (or reverse resample)
问题描述
我想对倒计时的熊猫时间序列重新采样.例如,让我们设置一个简单的11天时间序列:
I want to resample a pandas time series counting backwards. For example, let's set up a simple time series of 11 days:
>>> index = pd.date_range('01-01-2018', '01-11-2018', freq='D')
>>> randint = np.random.randint(low=0, high=9, size=(len(index), 1))
>>> df = pd.DataFrame(randint, index=index, columns=['random'])
>>> print(df)
random
2018-01-01 8
2018-01-02 8
2018-01-03 1
2018-01-04 4
2018-01-05 3
2018-01-06 5
2018-01-07 2
2018-01-08 6
2018-01-09 5
2018-01-10 1
2018-01-11 3
默认的熊猫行为
如果每隔5天重新采样一次,我会得到:
Default pandas behavior
If I resample it every 5 days, I'd get:
>>> df_5d = df.resample('5D').sum()
>>> print(df_5d)
random
2018-01-01 24
2018-01-06 19
2018-01-11 3
基本上,您有3个分组:前两个组有5个成员,最后一个组有1个,总共总共11个成员:
Basically you have 3 groupings: the first two groups have 5 members and the last group has 1, for a total of 11 members overall:
Start End
2018-01-01 2018-01-05
2018-01-06 2018-01-10
2018-01-11 2018-01-11
我想要的是这个
>>> df_5d = df.resample('5D').sum()
>>> print(df_5d)
random
2018-01-01 8
2018-01-02 21
2018-01-07 17
并且分组如下所示.查看我如何从最近的日期开始倒数'5D'
:
And the groupings are shown below. See how I counted '5D'
backwards starting from the latest date:
Start End
2018-01-01 2018-01-01
2018-01-02 2018-01-06
2018-01-07 2018-01-11
如何对倒计时的熊猫时间序列重新采样?
How do I resample a pandas time series counting backwards?
推荐答案
一种解决方法是将原始的df
一分为二,以便能够使用标准的重采样,然后pd.concat
都重新采样数据帧,例如:
A workaround could be to divise your original df
in two, to be able to use the standard resampling, then pd.concat
both resampled dataframes, such as:
res_interval = 5
df_res = pd.concat([df[:len(df)%res_interval].resample('{}D'.format(res_interval)).sum(),
df[len(df)%res_interval:].resample('{}D'.format(res_interval)).sum()])
加上我的随机数,我得到:
and with my random number, I get:
random
2018-01-01 1
2018-01-02 13
2018-01-07 26
这篇关于 pandas 重新采样时间序列向后计数(或反向重新采样)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!