根据不建议使用的警告修改OHLC重采样代码 [英] Modify OHLC resample code as per deprecated warning
问题描述
问题:
在处理市场数据并将日内数据重新采样到每日时间范围时,如下所示:
When working with market data and resampling intra-day data to the daily timeframe as follows:
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
似乎给了我我需要的输出(仍然需要验证)...
Which seems to gives me the output I need (still need to verify)...
我收到以下警告:
FutureWarning: how in .resample() is deprecated
the new syntax is .resample(...)..apply(<func>)
问题:
如何使用新语法复制resample
代码以与使用apply
的当前最佳实践保持一致?
How would this resample
code be replicated using the new syntax to align with the current best practice using apply
?
我尝试过的事情:
仅以data ['Low']为例:
Just using data['Low'] as an example:
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
没有给我相同的结果,我不确定在哪里插入apply
.
Does not give me the same results and Im not sure where to insert the apply
.
这是数据的一部分,用于测试是否必填:
Here is a slice of the data to test this with if required:
谢谢
推荐答案
.resample()
的作用类似于groupby
,因此您可以将该字典传递给resample().agg()
:
.resample()
works like groupby
so you can pass that dictionary to 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
这篇关于根据不建议使用的警告修改OHLC重采样代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!