BenP BenP - 1 month ago 9
Python Question

pandas timeseries DF slice and selection

I have a df with time series index at 15 minute frequency for 10 years:

dat
2004-04-07 00:00:00 4.0
2004-04-07 00:15:00 1.0
2004-04-07 00:30:00 2.0


I need to be able to extract the min & max 'dat' values per day (0-24 hours) and append them to seperate lists.

My plan was to use an datetime index and loop through like:

days = pd.to_datetime(pd.date_range(start = s, end = e, freq='1d'), format='%Y%m%d')

for day in days:
mn.append(min(df.loc[day].dat))


However the timestamps created in 'days' have hours (despite format) so it returns 00:00:00 hour value each time only:

2004-04-07 00:00:00 4.0
mn[0] '4.0'

Answer

I think you can use resample with Resampler.aggregate:

df = df.resample('D').agg({'dat': ['min','max']})
print (df)
            dat     
            min  max
2004-04-07  1.0  4.0

Also:

df = df.resample('D')['dat'].agg({'min_dat': 'min', 'max_dat': 'max'})
print (df)
            max_dat  min_dat
2004-04-07      4.0      1.0

And if need create lists use tolist:

Lmin = df.min_dat.tolist()
print (Lmin)
[1.0]

Lmax = df.max_dat.tolist()
print (Lmax)
[4.0]