bhn
bhn

Reputation: 51

Get min values between hours for each day

I have a pandas time series dataframe with a value for each hour of the day over an extended period, like this:

                     value
datetime                  
2018-01-01 00:00:00     38
2018-01-01 01:00:00     31
2018-01-01 02:00:00     78
2018-01-01 03:00:00     82
2018-01-01 04:00:00     83
2018-01-01 05:00:00     95
...

I want to create a new dataframe with the minimum value between hours 01:00 - 04:00 for each day but can't figure out how to do this.. the closest i can think of is:

df2 = df.groupby([pd.Grouper(freq='d'), df.between_time('01:00', '04:00')]).min())) 

but that gives me:

ValueError: Grouper for '' not 1-dimensional

Upvotes: 1

Views: 532

Answers (1)

jezrael
jezrael

Reputation: 862611

Use DataFrame.between_time with DataFrame.resample:

df = df.between_time('01:00', '04:00').resample('d').min()
print (df)
            value
datetime         
2018-01-01     31

Your solution is very close, only chain functions differently:

df = df.between_time('01:00', '04:00').groupby(pd.Grouper(freq='d')).min()
print (df)
            value
datetime         
2018-01-01     31

Upvotes: 2

Related Questions