如何使用熊猫将1M OHLC转换为5M OHLC

时间:2019-08-08 12:18:41

标签: python pandas csv resampling

我正在尝试使用熊猫将一堆文件的1M OHLC转换为5M OHLC

这是我的数据的样子:

dateTime             | open   | high | low | close | vol |
-----------------------------------------------------------
01-06-2018  00:50:00 | 0.97456| 0.2456|0.2145|0.241|54.26
01-06-2018  00:51:00 | 0.94566| 0.2145|0.1455|0.214|65.24
01-06-2018  00:52:00 | 0.89654| 0.2145|0.2144|0.214|73.25

如何重新采样并另存为5M OHLC CSV

预先感谢

编辑1:这就是我通过打印(df.info())所得到的

<class 'pandas.core.frame.DataFrame'>
Index: 375660 entries, 2018-06-01 00:00:00 to 2019-05-31 20:59:00
Data columns (total 4 columns):
open     375660 non-null float64
high     375660 non-null float64
low      375660 non-null float64
close    375660 non-null float64
dtypes: float64(4)
memory usage: 14.3+ MB
None

1 个答案:

答案 0 :(得分:1)

Resampler.agg与字典一起用于具有5T5 minutes聚合的列名称:

d = {'open':'first', 'high':'max','low':'min','close':'last','vol':'sum'}

df['dateTime'] = pd.to_datetime(df['dateTime'])
df = df.resample('5T', on='dateTime').agg(d)
print (df)
                        open    high     low  close     vol
dateTime                                                   
2018-01-06 00:50:00  0.97456  0.2456  0.1455  0.214  192.75