简体   繁体   中英

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

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. really confused with resample function, to_period...etc.

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):

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,

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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