首页 > 解决方案 > 无法将每日数据重新采样为每周并且无法应用逻辑

问题描述

我正在尝试使用以下代码将每日 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 天 - 没有应用任何逻辑。我怎样才能成功地重新采样到每周并应用逻辑?

标签: pythonpandaspandas-resample

解决方案


你错过了一个作业:

BTC = BTC.resample('W').apply(logic) # <-- here
BTC.index -= to_offset("6D")
BTC.head()

分析财务数据时的另一条建议:使用Adjusted Close列而不是Close考虑拆分、股息等。


推荐阅读