简体   繁体   English

熊猫重采样错误?

[英]Pandas resample bug?

Trying to down sample of 8 weekly time points to 2 points, each represents the average over 4 weeks, I use resample(). 尝试将每周8个时间点的样本减少到2个点,每个点代表4周内的平均值,我使用resample()。 I started by defining the rule using (60*60*24*7*4) seconds, and saw I ended up in 3 time points, latest one is dummy. 我首先使用(60 * 60 * 24 * 7 * 4)秒来定义规则,然后看到我在3个时间点结束,最后一个是假的。 Started to check it, I noticed that if I define the rule as 4W or 28D it's fine, but going down to 672H or smaller units (minutes, seconds,..) the extra faked column appears. 开始检查它时,我注意到如果将规则定义为4W或28D很好,但是下降到672H或更小单位(分钟,秒..),则会出现额外的伪造列。 This testing code: 此测试代码:

import numpy as np
import pandas as pd

d = np.arange(16).reshape(2, 8)
res = []

for month in range(1,13):
    start_date = str(month) + '/1/2014'
    df = pd.DataFrame(data=d, index=['A', 'B'], columns=pd.date_range(start_date, periods=8, freq='7D'))
    print(df, '\n')

    dfw = df.resample(rule='4W', how='mean', axis=1, closed='left', label='left')
    print('4 Weeks:\n', dfw, '\n')
    dfd = df.resample(rule='28D', how='mean', axis=1, closed='left', label='left')
    print('28 Days:\n', dfd, '\n')
    dfh = df.resample(rule='672H', how='mean', axis=1, closed='left', label='left')
    print('672 Hours:\n', dfh, '\n')
    dfm = df.resample(rule='40320T', how='mean', axis=1, closed='left', label='left')
    print('40320 Minutes:\n', dfm, '\n')
    dfs = df.resample(rule='2419200S', how='mean', axis=1, closed='left', label='left')
    print('2419200 Seconds:\n', dfs, '\n')
    res.append(([start_date], dfh.shape[1] == dfd.shape[1]))

print('\n\n--------------------------\n\n')
[print(res[i]) for i in range(12)]
pass

is printed as (I pasted here only the printout of the last iteration): 打印为(我在这里仅粘贴上次迭代的打印输出):

    2014-11-01  2014-11-29  2014-12-27
A         1.5         5.5         NaN
B         9.5        13.5         NaN

   2014-12-01  2014-12-08  2014-12-15  2014-12-22  2014-12-29  2015-01-05  \
A           0           1           2           3           4           5
B           8           9          10          11          12          13

   2015-01-12  2015-01-19
A           6           7
B          14          15

4 Weeks:
    2014-11-30  2014-12-28
A         1.5         5.5
B         9.5        13.5

28 Days:
    2014-12-01  2014-12-29
A         1.5         5.5
B         9.5        13.5

672 Hours:
    2014-12-01  2014-12-29  2015-01-26
A         1.5         5.5         NaN
B         9.5        13.5         NaN

40320 Minutes:
    2014-12-01  2014-12-29  2015-01-26
A         1.5         5.5         NaN
B         9.5        13.5         NaN

2419200 Seconds:
    2014-12-01  2014-12-29  2015-01-26
A         1.5         5.5         NaN
B         9.5        13.5         NaN



--------------------------


(['1/1/2014'], False)
(['2/1/2014'], True)
(['3/1/2014'], True)
(['4/1/2014'], True)
(['5/1/2014'], False)
(['6/1/2014'], False)
(['7/1/2014'], False)
(['8/1/2014'], False)
(['9/1/2014'], False)
(['10/1/2014'], False)
(['11/1/2014'], False)
(['12/1/2014'], False)

So there is an error for date_range starting on beginning of 9 months, and no error for 3 months (February-April). 因此,从9个月开始的date_range出现错误,并且3个月(2月至4月)没有错误。 Either I miss something or it's a bug, is it? 我错过了一些东西还是一个错误,是吗?

感谢@DSM和@Andy,的确我有熊猫0.15.1,升级到最新的0.15.2解决了它

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

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