问题:
处理市场数据并将日内数据重新采样到每日时间范围时,如下所示:
ohlc_dict = {
'Open':'first',
'High':'max',
'Low':'min',
'Last': 'last',
'Volume': 'sum'}
data.resample('1D',how=ohlc_dict).tail().dropna()
Open High Last Low Volume
Timestamp
2016-12-27 163.55 164.18 164.11 163.55 144793.00
2016-12-28 164.18 164.33 164.22 163.89 215288.00
2016-12-29 164.44 164.65 164.49 164.27 245538.00
2016-12-30 164.55 164.56 164.18 164.09 286847.00
Run Code Online (Sandbox Code Playgroud)
这似乎给了我需要的输出(仍需要验证)...
我收到以下警告:
FutureWarning: how in .resample() is deprecated
the new syntax is .resample(...)..apply(<func>)
Run Code Online (Sandbox Code Playgroud)
题:
如何resample
使用新语法复制此代码以与当前使用的最佳实践保持一致apply
?
我尝试过的:
仅使用数据['低']作为示例:
def ohlc (df):
return df['Low'].min()
data.resample('1D').dropna().apply(ohlc,axis=1).tail(2)
Timestamp
2016-12-29 164.45
2016-12-30 164.26
dtype: float64
Run Code Online (Sandbox Code Playgroud)
不给我相同的结果,我不知道在哪里插入apply
.
如果需要,以下是要测试的数据片段:
谢谢
.resample()
像groupby
这样工作,您可以将该字典传递给resample().agg()
:
df.resample('1D').agg(ohlc_dict).tail().dropna()
Out:
Volume Last High Open Low
Timestamp
2016-12-27 144793.0 164.11 164.18 163.55 163.55
2016-12-28 215288.0 164.22 164.33 164.18 163.89
2016-12-29 245538.0 164.49 164.65 164.44 164.27
2016-12-30 286847.0 164.18 164.56 164.55 164.09
Run Code Online (Sandbox Code Playgroud)