熊猫数据帧如何在时间序列数据中从一个时间帧获取数据到另一个1分钟时间帧

时间:2019-03-24 13:50:08

标签: python-3.x pandas

对于以下数据,如何将时间序列数据从一个时间段更改为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 ...等混淆。

2 个答案:

答案 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