-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
Closed
Labels
Closing CandidateMay be closeable, needs more eyeballsMay be closeable, needs more eyeballsDatetimeDatetime data dtypeDatetime data dtypeEnhancementFrequencyDateOffsetsDateOffsets
Description
It'd be nice if there was a way to easily broadcast arithmetic operations on Series/DataFrames with DatetimeIndexes
. For example, say you have monthly data, calculate the monthly mean, and want to multiply the original series by the monthly factor.
import numpy as np
import pandas as pd
idx = index=pd.date_range('1970-01-01', end='2015-01-01', freq='MS')
s = pd.Series(np.random.randn(len(idx)), name='ts',
index=idx)
mf = s.groupby(lambda x: x.month).mean()
In [3]: s
Out[3]:
1970-01-01 -1.032080
1970-02-01 -0.706686
1970-03-01 -0.380895
1970-04-01 0.322074
1970-05-01 -1.545298
...
2014-09-01 -0.787646
2014-10-01 -2.444045
2014-11-01 -0.646474
2014-12-01 -0.291925
2015-01-01 0.399430
Freq: MS, Name: ts, dtype: float64
In [4]: mf
Out[4]:
1 0.184326
2 -0.069534
3 -0.146372
4 0.018643
5 0.050393
...
8 -0.088188
9 0.148328
10 0.105042
11 0.219308
12 0.000997
Name: ts, dtype: float64
Right now you need something like (this could be simplified)
tmp = s.copy()
tmp.index = s.index.month
tmp = tmp * mf
tmp.index = s.index
s
It'd be nice to use the fact that s
is a datetimeindex to allow some broadcasting over the years. Something like
s.mul(mf, freq='M')
A similar case is when both s
and mf
have DatetimeIndexes. Then s.mul(mf, freq='M')
upsamples the lower frequency mf
to a monthly frequency, fills, and multiplies.
Another example from SO
Metadata
Metadata
Assignees
Labels
Closing CandidateMay be closeable, needs more eyeballsMay be closeable, needs more eyeballsDatetimeDatetime data dtypeDatetime data dtypeEnhancementFrequencyDateOffsetsDateOffsets