seasonal_decompose: operands could not be broadcast together with shapes on a series
Asked Answered
M

2

8

I know there are many questions on this topic, but none of them helped me to solve this problem. I'm really stuck on this.

With a simple series:

0
2016-01-31  266
2016-02-29  235
2016-03-31  347
2016-04-30  514
2016-05-31  374
2016-06-30  250
2016-07-31  441
2016-08-31  422
2016-09-30  323
2016-10-31  168
2016-11-30  496
2016-12-31  303

import statsmodels.api as sm
logdf = np.log(df[0])
decompose = sm.tsa.seasonal_decompose(logdf,freq=12, model='additive')
decomplot = decompose.plot()

i keep getting: ValueError: operands could not be broadcast together with shapes (12,) (14,)

I've tried pretty much everything, passing only logdf.values, passing a non-log series. It doesn't work. Numpy and statsmodel versions:

print(statsmodels.__version__)
print(pd.__version__)
print(np.__version__)
0.6.1
0.18.1
1.11.3
Magi answered 21/2, 2017 at 10:34 Comment(6)
What is the zero in front of your data series? Also, we cannot estimate a seasonal effect if there is only one season. There is no way to distinguish season from trend or other effects.Ladner
The 0 above the dates I think just means he has a pandas Series, indexed on those dates.Isidraisidro
Max is right, pandas series indexed on those dates. Why we cannot estimate a seasonal.effect with only one season? Anyway is just a sample series, i got a larger dataset grouped by dates (actually resampled), and still i got this error (should be numpy related i guess)Magi
Did you ever found out the issue with this? I'm running against this very annoying error too.Baum
Same problem here tooGazette
I think freq parameter should be smaller than the time series length. It's not the version problem.Runstadler
C
4

As @yoonforh pointed, in my case this was fixed by setting the freq parameter to less than the time series length. E.g. if your time series ts looks like this:

2014-01-01    0.0
2014-02-01    0.0
2014-03-01    1.0
2014-04-01    1.0
2014-05-01    0.0
2014-06-01    1.0
2014-07-01    1.0
2014-08-01    0.0
2014-09-01    0.0
2014-10-01    1.0
2014-11-01    0.0
2014-12-01    0.0

the shape is

(12,)

so this will give the error as per above:

seasonal_decompose(ts, freq=12, model='additive')

but if I try freq=11 or any other int less than 12, e.g.

seasonal_decompose(ts, freq=11, model='additive')

this works

Cementum answered 15/5, 2018 at 13:38 Comment(0)
M
0

i noticed that with newer pandas and statsmodel versions it seems to work.

Given a series:

2016-01-03    8.326275
2016-01-10    8.898229
2016-01-17    8.754792
2016-01-24    8.658172
2016-01-31    8.731659
2016-02-07    9.047233
2016-02-14    8.799662
2016-02-21    8.783549
2016-02-28    8.782783
2016-03-06    9.081825
2016-03-13    8.737934
2016-03-20    8.658693
2016-03-27    8.666475
2016-04-03    9.029178
2016-04-10    8.781555
2016-04-17    8.720787
2016-04-24    8.633909
2016-05-01    8.937744
2016-05-08    8.804925
2016-05-15    8.766862
2016-05-22    8.651899
2016-05-29    8.653645
...

And pd/sm version:

statsmodels.__version__ 0.8.0
pandas.__version__ 0.20.1

This is the result:

import statsmodels.api as sm
logdf = np.log(df_series)
decompose = sm.tsa.seasonal_decompose(logdf, model='additive', filt=None, freq=1, two_sided=True)
decompose.plot()

enter image description here

I hope this could solve your problem too.

Magi answered 2/10, 2017 at 9:25 Comment(1)
doesn't setting freq=1 undermines the sense of decomposition? If I understand correctly, seasional and residual components will always be emptyMalocclusion

© 2022 - 2024 — McMap. All rights reserved.