Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group by date range in pandas dataframe

Tags:

python

pandas

I have a time series data in pandas, and I would like to group by a certain time window in each year and calculate its min and max.

For example:

times = pd.date_range(start = '1/1/2011', end = '1/1/2016', freq = 'D')
df = pd.DataFrame(np.random.rand(len(times)), index=times, columns=["value"])

How to group by time window e.g. 'Jan-10':'Mar-21' for each year and calculate its min and max for column value?

like image 438
Jason Avatar asked Dec 29 '25 09:12

Jason


1 Answers

You can use the resample method.

df.resample('5d').agg(['min','max'])
like image 142
Dallas Lindauer Avatar answered Dec 31 '25 23:12

Dallas Lindauer