繁体   English   中英

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

[英]Pandas dataframe how to get data from one time frame to another 1 min time frame in Time series data

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

如果要在重新采样后获得正确的OHLC值,则需要应用适当的聚合函数( first对Open进行取值,对High取max ,对Low取minlast对Close取值):

df.resample('1T').agg({
    'Open': 'first',
    'High': 'max',
    'Low': 'min',
    'Close': 'last'})

输出:

                       Open    High     Low    Close
DateTime                                            
2019-03-22 09:15:00  1342.0  1349.0  1342.0  1344.45
2019-03-22 09:16:00  1344.4  1344.4  1344.4  1344.40

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM