BenP BenP - 1 year ago 137
Python Question

pandas timeseries DF slice and selection

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

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:

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 Source

I think you can use resample with Resampler.aggregate:

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


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)

Lmax = df.max_dat.tolist()
print (Lmax)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download