Get the average year (average number of days over several years) in Pandas

I am new to Pandas timeseries and dataframes and struggling to complete this simple task. I have data "data" (1-dimensional array float32-Numpy) for each day from 01/01/2004 to 12/31/2008. Dates are stored as a list of date and time date objects. In principle, I would like to calculate the full "standard year" - the average value of each day of all years (1-365). I started with this similar (?) Question ( Getting the average value of a specific hour on weekdays for several years in the Pandas data framework ), but I couldnโ€™t get to the desired result - a time series of 365 โ€œaverageโ€ days, for example, for all four January 1, January 2 ...

A small example script:

import numpy as np import pandas as pd import datetime startdate = datetime.datetime(2004, 1, 1) enddate = datetime.datetime(2008, 1, 1) days = (enddate + datetime.timedelta(days=1) - startdate).days data = np.random.random(days) dates = [startdate + datetime.timedelta(days=x) for x in range(0, days)] ts = pd.Series(data, dates) test = ts.groupby(lambda x: (x.year, x.day)).mean() 
+5
source share
1 answer

A group for a month and a day, not a year and a day:

 test = ts.groupby([ts.index.month, ts.index.day]).mean() 

gives

 1 1 0.499264 2 0.449357 3 0.498883 ... 12 17 0.408180 18 0.317682 19 0.467238 ... 29 0.413721 30 0.399180 31 0.828423 Length: 366, dtype: float64 
+6
source

Source: https://habr.com/ru/post/1212555/


All Articles