python - Resample pandas DataFrame of OHLC data into three-day bars ignoring weekends
问题描述
I have daily price data with Open, High, Low, Close and the date as the index. I want to create three-day bars, but I want the bars to "wrap around the weekend," so that I end up with MTuW, TuWTh, WThF, ThFM, and FMTu bars.
(MTuW = Monday Tuesday Wednesday)
(FMTu = Friday Monday Tuesday)
(FSaSu = Friday Saturday Sunday)
I tried
df = df.asfreq('B')
df = df.resample('3D').mean()
But that seems to work properly only for MTuW, TuWTh, and WThF bars. The other bars don't seem to skip the weekend days, meaning that what is intended to be a FMTu bar is actually a FSaSu bar, or more accurately simply a Friday bar.
I believe there is a way to make this work perhaps with
from pandas.tseries.offsets import BDay
But it's not clear to me exactly how to make it work.
解决方案
I didn't test this but I think you just need to use:
df = df.resample('3B').mean()
Update
To also accommodate for holidays, use a custom rule to resample:
from pandas.tseries.offsets import CustomBusinessDay
from pandas.tseries.holiday import USFederalHolidayCalendar
bday_us = CustomBusinessDay(calendar=USFederalHolidayCalendar())
df = df.resample(bday_us).mean()
I used USFederalHolidayCalendar
here. To get an overview of the holidays in such a holiday class:
>>> pprint(USFederalHolidayCalendar().rules)
[Holiday: New Years Day (month=1, day=1, observance=<function nearest_workday at 0x10db3ce18>),
Holiday: Dr. Martin Luther King Jr. (month=1, day=1, offset=<DateOffset: weekday=MO(+3)>),
Holiday: Presidents Day (month=2, day=1, offset=<DateOffset: weekday=MO(+3)>),
Holiday: MemorialDay (month=5, day=31, offset=<DateOffset: weekday=MO(-1)>),
Holiday: July 4th (month=7, day=4, observance=<function nearest_workday at 0x10db3ce18>),
Holiday: Labor Day (month=9, day=1, offset=<DateOffset: weekday=MO(+1)>),
Holiday: Columbus Day (month=10, day=1, offset=<DateOffset: weekday=MO(+2)>),
Holiday: Veterans Day (month=11, day=11, observance=<function nearest_workday at 0x10db3ce18>),
Holiday: Thanksgiving (month=11, day=1, offset=<DateOffset: weekday=TH(+4)>),
Holiday: Christmas (month=12, day=25, observance=<function nearest_workday at 0x10db3ce18>)]
Obviously such a holiday object can be customised and built from scratch too. More info on holiday classes: https://pandas.pydata.org/pandas-docs/stable/timeseries.html#holidays-holiday-calendars
推荐阅读
- django - Django中预保存、保存后、预删除、删除后信号的典型用例是什么?
- amazon-cloudwatchlogs - 适用于 AND 的 AWS CloudWatch Logs 过滤器表达式
- iis - 升级到 TFS 2017 HTTP 错误 401.1 - 未经授权
- winforms - Powershell - 将富文本数据显示为格式化文本而不加载外部文件 - WinForms 或 XAML
- excel - 在 A 列中选择包含内容的单元格?
- node.js - 在 Mongoose 模式中禁用插件
- docker - Jenkins Golang 声明式管道:构建 Docker 映像并推送到 Docker Hub
- python - localhost上的Python计算器 - 获取结果时出现意外结果
- regex - 正则表达式:在双引号之间查找分号
- go - 在顺序执行之前等待通道中的 N 个项目