对于以下数据,如何将时间序列数据从一个时间段更改为1分钟时间段
时间序列数据:
Open High Low Close
DateTime
2019-03-22 09:15:00 1342 1342 1342 1342
2019-03-22 09:15:09 1344 1344 1344 1344
2019-03-22 09:15:12 1344.4 1344.4 1344.4 1344.4
2019-03-22 09:15:17 1345 1345 1345 1345
2019-03-22 09:15:22 1344.4 1345.4 1344.4 1344.4
2019-03-22 09:15:24 1349 1349 1349 1349
2019-03-22 09:15:32 1346 1346 1346 1346
2019-03-22 09:15:36 1346 1346 1346 1346
2019-03-22 09:15:41 1346.25 1346.25 1346.25 1346.25
2019-03-22 09:15:43 1346.25 1346.25 1346.25 1346.25
2019-03-22 09:15:45 1346 1346 1346 1346
2019-03-22 09:15:55 1344.45 1344.45 1344.45 1344.45
2019-03-22 09:16:00 1344.4 1344.4 1344.4 1344.4
我希望有1分钟的时间范围数据。确实与重采样功能,to_period ...等混淆。
答案 0 :(得分:2)
如果要在重采样后获得正确的OHLC值,则需要应用适当的聚合函数(将first
用于Open,将max
用于High,将min
用于Low,将{{ 1}}(关闭):
last
输出:
df.resample('1T').agg({
'Open': 'first',
'High': 'max',
'Low': 'min',
'Close': 'last'})
答案 1 :(得分:0)
Resample返回一个Resampler对象,您在该对象上应用了聚合函数,
df.resample('1T').last()
Open High Low Close
DateTime
2019-03-22 09:15:00 1344.45 1344.45 1344.45 1344.45
2019-03-22 09:16:00 1344.40 1344.40 1344.40 1344.40
如果您只想更改期间而不希望汇总值,请使用to_period
df.to_period('1T')
Open High Low Close
DateTime
2019-03-22 09:15 1342.00 1342.00 1342.00 1342.00
2019-03-22 09:15 1344.00 1344.00 1344.00 1344.00
2019-03-22 09:15 1344.40 1344.40 1344.40 1344.40
2019-03-22 09:15 1345.00 1345.00 1345.00 1345.00
2019-03-22 09:15 1344.40 1345.40 1344.40 1344.40
2019-03-22 09:15 1349.00 1349.00 1349.00 1349.00
2019-03-22 09:15 1346.00 1346.00 1346.00 1346.00
2019-03-22 09:15 1346.00 1346.00 1346.00 1346.00
2019-03-22 09:15 1346.25 1346.25 1346.25 1346.25
2019-03-22 09:15 1346.25 1346.25 1346.25 1346.25
2019-03-22 09:15 1346.00 1346.00 1346.00 1346.00
2019-03-22 09:15 1344.45 1344.45 1344.45 1344.45
2019-03-22 09:16 1344.40 1344.40 1344.40 1344.40