Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multi-monthly mean with pandas' Series

I have a sequence of datetime objects and a series of data which spans through several years. A can create a Series object and resample it to group it by months:

df=pd.Series(varv,index=dates)
multiMmean=df.resample("M", how='mean')
print multiMmean

This, however, outputs

2005-10-31    172.4
2005-11-30     69.3
2005-12-31    187.6
2006-01-31    126.4
2006-02-28    187.0
2006-03-31    108.3
...
2014-01-31     94.6
2014-02-28     82.3
2014-03-31    130.1
2014-04-30     59.2
2014-05-31     55.6
2014-06-30      1.2

which is a list of the mean value for each month of the series. This is not what I want. I want 12 values, one for every month of the year with a mean for each month through the years. How do I get that for multiMmean?

I have tried using resample("M",how='mean') on multiMmean and list comprehensions but I cannot get it to work. What am I missing?

Thank you.

like image 502
TomCho Avatar asked May 07 '15 18:05

TomCho


1 Answers

the following worked for me:

# create some random data with datetime index spanning 17 months
s = pd.Series(index=pd.date_range(start=dt.datetime(2014,1,1), end = dt.datetime(2015,6,1)), data = np.random.randn(517))

In [25]:
# now calc the mean for each month
s.groupby(s.index.month).mean()
Out[25]:
1     0.021974
2    -0.192685
3     0.095229
4    -0.353050
5     0.239336
6    -0.079959
7     0.022612
8    -0.254383
9     0.212334
10    0.063525
11   -0.043072
12   -0.172243
dtype: float64

So we can groupby the month attribute of the datetimeindex and call mean this will calculate the mean for all months

like image 150
EdChum Avatar answered Oct 22 '22 04:10

EdChum