在Python中,我想使用pandas库将我的分钟数据转换为日常数据。在
加载数据(从csv)并将DatetimeIndex
作为索引后,对象xx
如下所示:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 540949 entries, 2007-01-02 09:46:00+08:00 to 2013-10-17 16:15:00+08:00
Data columns (total 5 columns):
Open 540949 non-null values
High 540949 non-null values
Low 540949 non-null values
Close 540949 non-null values
Volume 540949 non-null values
dtypes: int64(5)
我现在想把这些日内数据转换成每日OHLC数据。在
我最初的尝试如下:
^{pr2}$有没有更有效/更有说服力的方法?在
注意
我刚刚找到了这个方法…它更简洁…但是有没有一种方法可以使用resample
并且如果使用这个方法,我可以将一个分钟的OHLC时间序列转换成15分钟的时间序列OHLC?(注意,可能会少了一些分钟,所以每15行分开是行不通的…)
def ohlcsum(df):
df = df.sort()
return {
'Open': df['Open'][0],
'High': df['High'].max(),
'Low': df['Low'].min(),
'Close': df['Close'][-1],
'Volume': df['Volume'].sum()
}
xx.groupby('date').agg(ohlcsum)
也不愿意写这样的东西来做一个日期栏
xx['date'] = [i.date() for i in xx.index]
是否可以使用TimeGrouper('1D')
函数来按日期分组?在
我试着用下面的方法,但由于某些原因它似乎不起作用。。。在
xx.groupby(TimeGrouper('1D')).agg(ohlcsum)
任何帮助都将不胜感激。。。在
在master/0.13(很快发布)中,您可以这样做(在0.12中,这是一个手动操作,因为您必须在系列中单独执行)
这将在0.12下工作
^{pr2}$我对pandas和python还很陌生,但我想出了一个允许转换到任何时间段的方法
在我的例子中,minData是分钟数据,以没有任何逗号的平面格式存储。我的数据来自quantquote.com网站在
我相信一定有一个更简洁的方法,但这与我的数据一起工作,它允许我使用相同的代码生成15分钟,1小时,每天,每周和每月。而且速度很快。在
如有任何改进/意见,我们将不胜感激。在
谨致问候
-杰森
相关问题 更多 >
编程相关推荐