【发布时间】:2016-05-20 22:36:29
【问题描述】:
我从以月末为时间戳的月度系列开始。我想通过填充远期值将它们上采样到业务(周一至周五)的每日频率。 我希望 2 个条件为真:
- 如果在原始数据中是周末,则在重新采样时绝不会丢失值 时间序列
- 总是向前填充:如果原始系列中的 EOM 日期是 星期六,我希望该观察结果显示为下星期一 日常系列
虽然不优雅,但我认为最安全的方法是:
daily_series = monthly_series.resample(rule='D').ffill().resample(rule='B',how='first')
现在,意外情况:
dates = ['1953-02-28', '1953-03-31', '1953-04-30', '1953-05-31']
# '1953-02-28' was a Saturday
values = [1,2,3,4]
monthly_ts = pd.Series(values, index = dates)
monthly_ts
Out[74]:
1953-02-28 1
1953-03-31 2
1953-04-30 3
1953-05-31 4
dtype: int64
daily_ts = monthly_ts.resample(rule='D').ffill().resample(rule='B',how='first')
Out[77]:
1953-02-27 1 # Why do I have this observation?
1953-03-02 1
1953-03-03 1
1953-03-04 1
在重采样中使用周六的观测值作为周五。 这发生在 .resample(rule = 'B')
您能否向我解释一下为什么会发生这种情况以及如何防止这种情况发生?
【问题讨论】:
标签: python pandas resampling