python - 无法将每日数据重新采样为每周并且无法应用逻辑
问题描述
我正在尝试使用以下代码将每日 OHLCV 数据转换为每周:
#resample so that Open is the first monday price, High and low are the weeks min and max and close is the last sunday price and volume is the sum (can be customized - pandas docs)
logic = {'Open' : 'first',
'High' : 'max',
'Low' : 'min',
'Close' : 'last',
'Volume': 'sum'}
from pandas.tseries.frequencies import to_offset
BTC.resample('W').apply(logic)
BTC.index -= to_offset("6D")
BTC.head()
这将打印一个数据框,它只是原始每日数据,日期索引偏移 6 天 - 没有应用任何逻辑。我怎样才能成功地重新采样到每周并应用逻辑?
解决方案
你错过了一个作业:
BTC = BTC.resample('W').apply(logic) # <-- here
BTC.index -= to_offset("6D")
BTC.head()
分析财务数据时的另一条建议:使用Adjusted Close
列而不是Close
考虑拆分、股息等。
推荐阅读
- ssl - DocuSign 是否需要更新其客户端根证书检查?
- shell - 如何使用AWK命令将column1,column3和column 9打印到csv的column12?
- json - 如何将 Json 值传递给 C# 中的存储过程
- ios - 按消息文本的 Pubnub 搜索频道
- mongodb - readStartDocument 只能在 CurrentBSONType 为 DOCUMENT 时调用
- html - 如何垂直对齐一个div?
- python - 如何将函数结果保存为 pandas 数据框?
- java - 尽管 xpath 准确,但无法单击元素
- python - QPainter.drawText 输出模糊
- sass - SCSS - 在文件范围内使用变量