1

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

1 Answer 1

4

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
3
  • Thanks for replying. Im getting this error: SpecificationError: Column(s) ['High', 'Last', 'Low', 'Open'] do not exist
    – Grantx
    Feb 12, 2021 at 15:17
  • @Grantx What is print(df. columns)? Error means columns names are not ` ['High', 'Last', 'Low', 'Open'] `
    – jezrael
    Feb 12, 2021 at 15:36
  • 1
    Sorry my mistake it does work. There was a space in front of each name (which is impossible to see). I deleted the space and works. THank you very much for the answer.
    – Grantx
    Feb 12, 2021 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.