Grantx
Grantx

Reputation: 345

Combine 5 minute timeframes into hourly

Is there a way to do the following: combine 5 minute data into hourly data and also combine open, high, low, last into hourly data

example of the 09:00 - 10:00 hour:

open = 3896 (open price at 09:00)

High = Highest from 09:00 to 09:55

Low = Lowest from 09:00 to 09:55

Last = 3902 (last price at 09:55)

Date         Time       Open     High    Low     Last
12/02/2021   09:00:00   3896    3899.25 3895.75 3898.75
12/02/2021   09:05:00   3898.5  3899    3898    3898.25
12/02/2021   09:10:00   3898    3899    3897.5  3898
12/02/2021   09:15:00   3898    3899.25 3897.5  3898.5
12/02/2021   09:20:00   3898.25 3900    3898.25 3899.25
12/02/2021   09:25:00   3899.25 3900.25 3899    3900
12/02/2021   09:30:00   3900    3901.5  3899    3899.75
12/02/2021   09:35:00   3899.75 3900.5  3899.5  3900
12/02/2021   09:40:00   3900    3901.5  3899.75 3901.5
12/02/2021   09:45:00   3901.25 3902.5  3901.25 3902
12/02/2021   09:50:00   3902    3902    3899.75 3901.5
12/02/2021   09:55:00   3901.25 3902.25 3901    3902
12/02/2021   10:00:00   3902    3903    3901.75 3903
12/02/2021   10:05:00   3903    3903.25 3902.25 3902.75
12/02/2021   10:10:00   3902.75 3902.75 3901.5  3901.5
12/02/2021   10:15:00   3901.5  3902.5  3901.25 3902.5
12/02/2021   10:20:00   3902.5  3903.75 3902.25 3903.75
12/02/2021   10:25:00   3903.75 3903.75 3902.25 3903.25
12/02/2021   10:30:00   3903.25 3903.25 3902.25 3903.25
12/02/2021   10:35:00   3903.25 3903.25 3902.25 3902.75
12/02/2021   10:40:00   3902.5  3903.5  3902    3903.25
12/02/2021   10:45:00   3903.25 3904.25 3902.75 3902.75
12/02/2021   10:50:00   3902.75 3903.25 3902    3902.75
12/02/2021   10:55:00   3902.75 3904.25 3902.75 3903.25
12/02/2021   11:00:00   3903.5  3904    3903    3904

Upvotes: 1

Views: 503

Answers (1)

jezrael
jezrael

Reputation: 862611

Create DatetimeIndex and then use Resampler.agg:

df.index = pd.to_datetime(df['Date'] + ' ' + df['Time'])

df = df.resample('H').agg({'Open':'first', 'High':'max', 'Low':'min', 'Last':'last'})
print (df.head())
                       Open     High      Low     Last
2021-12-02 09:00:00  3896.0  3902.50  3895.75  3902.00
2021-12-02 10:00:00  3902.0  3904.25  3901.25  3903.25
2021-12-02 11:00:00  3903.5  3904.00  3903.00  3904.00

Upvotes: 4

Related Questions