Reputation: 23
I'm new on python, and I'm trying to convert this code from another language. And I don't know if there is a simple way to solve my problem and avoid the long processing time.
About the problem
I have a data frame with 2 columns (time, for every 30 minutes; and a value) trying to find a maximum aggregate value from a specific time step for each day.
About the time, they are already an accumulation. For example, '2019-03-28 04:00:00', represents an accumulation from 03:31:00 to 04:00:00.
So, for a time step equals to 2 hours, for example, I may find the maximum value ranging from 04:00:00 to 05:30:00 (=80.0) at 2019-03-28, but it could happen in a different set of data.
Time Value
2019-03-28 00:30:00 10.0
2019-03-28 01:00:00 5.0
2019-03-28 01:30:00 0.0
2019-03-28 02:00:00 15.0
2019-03-28 02:30:00 2.0
2019-03-28 03:00:00 0.0
2019-03-28 03:30:00 0.0
2019-03-28 04:00:00 10.0 *
2019-03-28 04:30:00 0.0 *
2019-03-28 05:00:00 10.0 *
2019-03-28 05:30:00 60.0 *
2019-03-28 06:00:00 0.0
........
........
2019-03-28 23:30:00 0.0
........
........
EDIT Is there a simple way to automatically find the maximum value aggregating 2 hours for each day?
Upvotes: 2
Views: 602
Reputation: 5788
Using .resample()
:
# Import and initialise pacakages in session:
import pandas as pd
# Coerce Time to datetime: Time => Date Vector
df['Time'] = pd.to_datetime(df['Time'])
# Replace index with date vec: index => Time
df.set_index(df['Time'], inplace=True)
# Resample to get the daily max: stdout => aggregated Series
df.resample('D').max()
Upvotes: 1
Reputation: 26676
Please try the following. If doesn't work let us know we will help further
df['Time']=pd.to_datetime(df['Time'])#Coerce Time to datetime
df.set_index(df['Time'], inplace=True)#Set time as the index
df.groupby(df.index.date)['Value'].max().to_frame()#groupby date. Can also substitute date for day
Upvotes: 2