Repository navigation
Description
This issue might be related with this one: #2733
The function statsmodels.tsa.seasonal_decompose seems not to be working with 2D data (in this case, a numpy array with 34 rows and 108 columns). It raises the following error.
Traceback (most recent call last):
File "M:/Documents/workspace/Algorithms/TimeSeries/test_signal_decomposition.py", line 43, in <module>
res = sm.tsa.seasonal_decompose(df)
File "C:\Python34\lib\site-packages\statsmodels\tsa\seasonal.py", line 108, in seasonal_decompose
resid = detrended - seasonal
ValueError: operands could not be broadcast together with shapes (108,34) (108,)
Process finished with exit code 1
To give more context, my idea is doing the seasonal decomposition of each row and then do additional stuff with the seasonal component of the time series. Nothing very sophisticated at this moment. Each row represents a location in the Netherlands and each column a vegetation index per month, for the period 2006-2014. The type of the array is a float64. In this nice example I learnt that the seasonal_decompose function requires a DateTimeIndex and a different data organization, so I transposed my 34x108 matrix to a 108x34 one (I do not mind reading the results per column, instead of row), and added an temporal index as follows:
# This code raises the error above.
init_date = datetime.datetime(2006, 1, 1)
vegetation = np.array(lveg)[:,1:109].T
print(vegetation.dtype) # This prints type float64
df = pd.DataFrame(vegetation)
df.index = pd.DatetimeIndex(freq="m", start=init_date, periods=108)
res = sm.tsa.seasonal_decompose(df)
I tried to trace back the code in the file statsmodels.tsa.seasonal.py and apparently the program is capable of calculating the trend (shape (108, 34)), seasonal (shape (108, )) and detrended(shape (108, 34)) components of the time series, but fails in subtracting the seasonal component to the detrended one to obtain the residual. One of the developers suggests in this post that this function may only work for 1D arrays.
I guess I can run the line res = sm.tsa.seasonal_decompose(df) for each of the 34 locations (plus the DateTimeIndex) in my 2D array individually, but since I did not get any message on the console saying that the seasonal decomposition is only available with 1D arrays, I thought this might be a bug.
Thanks!
Activity
josef-pkt commentedon Feb 17, 2016
I already opened #2820 because I was curious how much needs to be changed. (I will close this one as duplicate, but it has more background on your usecase.)
josef-pkt commentedon Mar 29, 2016
duplicate