python - Pandas 重采样错误?

标签 python pandas resampling

尝试将每周 8 个时间点的样本减少到 2 个点,每个点代表 4 周内的平均值,我使用 resample()。我首先使用 (60*60*24*7*4) 秒定义规则,结果发现我最终出现了 3 个时间点,最新的一个是虚拟的。开始检查它,我注意到如果我将规则定义为 4W 或 28D 就可以了,但是下降到 672H 或更小的单位(分钟、秒……)就会出现额外的伪造列。本次测试代码:

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

打印为(我在这里仅粘贴了最后一次迭代的打印输出):

    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)

因此,从 9 月初开始的 date_range 存在错误,并且 3 个月(二月至四月)没有错误。要么我错过了什么,要么它是一个错误,是吗?

最佳答案

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

关于python - Pandas 重采样错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28332644/

相关文章:

python - 是什么导致 'unicode' 对象在 pyspark 中没有属性 'toordinal'?

python - Twisted AMP - 如何发送大于 64K 的值

基于电子邮件的python UUID

python - Pandas DataFrame.groupby 包括索引

python - 基于多索引 DataFrame 的水平重新设定价格的最 pythonic 方法是什么?

python - Pandas 重采样函数问题从分钟到毫秒的重新采样

python - 在 Python 中查找最接近的值对并计算平均值

pandas - 将字符串转换为整数 Pandas 数据帧索引

machine-learning - Weka 添加多个元过滤分类器

python - 获取时间戳在不规则时间间隔内的行 pandas (Time Series)