[英]Pandas Resampling Hourly OHLC to Daily OHLC
I have a dataframe of hourly OHLC as follows (please ignore the values of OHLC, I typed them in for better illustration),我有一个每小时 OHLC 的数据框,如下所示(请忽略 OHLC 的值,我输入了它们以获得更好的说明),
hr_df =
Close High Low Open
2017-09-04 05:00:00 0.715035 0.715035 0.715035 0.715035
2017-09-04 06:00:00 0.715035 0.715045 0.715015 0.715035
2017-09-04 07:00:00 0.715040 0.715050 0.714035 0.715035
:
:
2017-09-05 05:00:00 0.715045 0.715105 0.714985 0.715035
2017-09-05 06:00:00 0.715040 0.716045 0.714605 0.715035
2017-09-05 07:00:00 0.715040 0.717045 0.713225 0.715035
:
:
2017-09-06 05:00:00 0.715040 0.714045 0.713355 0.715035
I want to resample it into daily OHLC, example,我想将其重新采样为每日 OHLC,例如,
day_df =
Close High Low Open
2017-09-04 0.715035 0.715035 0.715035 0.715035
2017-09-05 0.715035 0.715045 0.715015 0.715035
2017-09-06 0.715040 0.715050 0.714035 0.715035
2017-09-07 0.715045 0.715105 0.714985 0.715035
2017-09-08 0.715040 0.716045 0.714605 0.715035
2017-09-09 0.715040 0.714045 0.713355 0.715035
2017-09-10 0.715040 0.717045 0.713225 0.715035
I tried using pandas resample method, day_df = hr_df.resample('D').pad()
or day_df = hr_df.resample('D').ohlc()
but it is not working.我尝试使用熊猫重新采样方法, day_df = hr_df.resample('D').pad()
或day_df = hr_df.resample('D').ohlc()
但它不起作用。 I know I am probably not using the proper method.我知道我可能没有使用正确的方法。 I will really appreciate it if someone can guide me to an alternative solution or the proper method to use.如果有人可以指导我找到替代解决方案或正确的使用方法,我将不胜感激。
I think you need downsample by Resampler.agg
by dictionary
with keys for column names and values for functions:我认为您需要通过Resampler.agg
通过dictionary
进行下Resampler.agg
,其中包含列名的键和函数的值:
day_df = (hr_df.resample('D')
.agg({'Open': 'first', 'High': 'max', 'Low': 'min', 'Close': 'last'}))
print (day_df)
Open High Close Low
2017-09-04 0.715035 0.715050 0.71504 0.714035
2017-09-05 0.715035 0.717045 0.71504 0.713225
2017-09-06 0.715035 0.714045 0.71504 0.713355
Try to use pd.Grouper
.尝试使用pd.Grouper
。 If pair is a forex pair for example you can use it as index together with the newly grouped date:例如,如果对是外汇对,您可以将其与新分组的日期一起用作索引:
hr_df.groupby([pd.Grouper(key='date',freq='D'), 'pair']).agg(
{'Open': 'first', 'High': 'max', 'Low': 'min', 'Close': 'last'}
) )
That works for me.这对我行得通。
quotes.set_index('end', inplace=True)
quotes.index = pd.to_datetime(quotes.index)
ohlc_dict = {
'open': 'first',
'high': 'max',
'low': 'min',
'close': 'last'
}
quotes.resample("1D", closed='left', label='left').apply(ohlc_dict).dropna()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.