Search code examples
pythonpandasgroup-bytime-seriespython-datetime

Timeseries - group data by specific time period slices


I am working with a csv file that has a dataset of 13 years worth of 5m time intervals.

I am trying to slice sections of this dataset into specific time periods.

example

time_period = (df['time'] >= '01:00:00') & (df['time']<='5:00:00')
time_period_df = df.loc[time_period]

I would expect an output of only the time between 1-5 to be included in this time period, however, I am getting all 24hrs in the output

I would like the output to print only time in between and including 1:00:00 and 5:00:00.


Solution

  • # First, extract the hour portion of the time strings
    df['hour'] = df['time'].str.slice(0, 2)
    
    # Next, create a boolean mask using the comparison operators on the 'hour' column
    time_period = (df['hour'] >= '01') & (df['hour'] <= '05')
    
    # Finally, use this boolean mask to create your time period dataframe
    time_period_df = df.loc[time_period]