简体   繁体   English

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

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

How to change the Timeseries data from one time period to 1 min time periods for below data 如何将以下数据的时间序列数据从一个时间段更改为1分钟时间段

Time Series Data: 时间序列数据:

                       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

I would like to have as 1min time frame data. 我希望有1分钟的时间范围数据。 really confused with resample function, to_period...etc. 确实与重采样功能,to_period ...等混淆。

If you want to have correct OHLC values after the resampling, you need to apply proper aggregation functions (taking first for Open, max for High, min for Low and last for Close): 如果要在重新采样后获得正确的OHLC值,则需要应用适当的聚合函数( first对Open进行取值,对High取max ,对Low取minlast对Close取值):

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

Output: 输出:

                       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 returns a Resampler object on which you apply the aggregate function, 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

If you only wish to change the period but not aggregate the values, use to_period 如果仅希望更改时间段而不希望汇总值,请使用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.

相关问题 如何将循环时间序列pandas数据框转换为pandas多索引数据框 - How to transform recurrent time series pandas data frame to pandas multi-index data frame 如何截断 Pandas 时间序列数据框中的列以删除前导零和尾随零? - How to truncate a column in a Pandas time series data frame so as to remove leading and trailing zeros? 如何在 Python 中转换数据帧以进行时间序列分析? - How to convert data frame for time series analysis in Python? 如何汇总一个Pandas Dataframe中时间序列数据的缺失值? - How to summarize missing values in time series data in a Pandas Dataframe? 如何更改 pandas 数据框中列的日期时间格式 - How to change date time format of column in pandas data frame pandas:从一个数据帧添加行到另一个数据帧? - pandas: Add row from one data frame to another data frame? 如何在特定时间范围内获取字典或数据集? - How to get a dictionary or set of data within a particular time frame? 如何将 Pandas 数据帧从一个文件读取到另一个文件 - How to read Pandas data frame from one file to another file Pandas:如何从另一个数据帧中获取出现次数? - Pandas: How to get count of occurrence from another data frame? 相当于for循环,可减少熊猫数据帧操作的执行时间 - For loop equivalent to reduce execution time in pandas data frame operation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM