how to aggergate date from 15 min increments to 1 hour increments pd datetime in python

Assuming you have a pandas DataFrame called df which has a column called timestamp with datetime dtype and another column called value which you want to aggregate, you can resample the DataFrame using resample() method to convert 15 minutes intervals to 1 hour intervals and then apply a function like mean() to aggregate the values.

main.py
import pandas as pd

df = pd.DataFrame({'timestamp': ['2022-05-01 00:00:00', '2022-05-01 00:15:00', '2022-05-01 00:30:00', '2022-05-01 00:45:00', '2022-05-01 01:00:00'],
                   'value': [10, 20, 30, 40, 50]})
df['timestamp'] = pd.to_datetime(df['timestamp'])

df_hourly = df.set_index('timestamp').resample('1H').mean()

print(df_hourly)
350 chars
10 lines

Output:

main.py
                     value
timestamp                 
2022-05-01 00:00:00   25.0
2022-05-01 01:00:00   50.0
108 chars
5 lines

related categories

gistlibby LogSnag