ValueError: You must specify a period or x must be a pandas object with a DatetimeIndex with a freq not set to None

Question:

Hello and thank you in advance for your help!

I am getting ValueError: You must specify a period or x must be a pandas object with a DatetimeIndex with a freq not set to None when I try to do a time series decomposition that pulls from GitHub. I think I have a basic understanding of the error, but I do not get this error when I directly pull the data from the file on my computer, instead of pulling from GitHub. Why do I only get this error when I pull my data from GitHub? And how should I change my code so that I no longer get this error?

import pandas as pd
import numpy as np 
%matplotlib inline
from statsmodels.tsa.seasonal import seasonal_decompose

topsoil = pd.read_csv('https://raw.githubusercontent.com/the- 
datadudes/deepSoilTemperature/master/meanDickinson.csv',parse_dates=True)

topsoil = topsoil.dropna()
topsoil.head()

topsoil.plot();

result = seasonal_decompose(topsoil['Topsoil'],model='ad')


from pylab import rcParams
rcParams['figure.figsize'] = 12,5
result.plot();
Asked By: Xavier Conzet

||

Answers:

Try this:

import pandas as pd
import numpy as np 
%matplotlib inline
from statsmodels.tsa.seasonal import seasonal_decompose

topsoil = pd.read_csv('https://raw.githubusercontent.com/the-datadudes/deepSoilTemperature/master/meanDickinson.csv',parse_dates=True)

topsoil = topsoil.dropna()
topsoil.head()

topsoil.plot();

topsoil['Date'] = pd.to_datetime(topsoil['Date'])
topsoil = topsoil.set_index('Date').asfreq('D')
result = seasonal_decompose(topsoil, model='ad')

from pylab import rcParams
rcParams['figure.figsize'] = 12,5
result.plot();

Output:

enter image description here

Answered By: Scott Boston

try adding this

freq=12, extrapolate_trend=12

Full code would look like:

from pylab import rcParams
import statsmodels.api as sm
rcParams['figure.figsize'] = 12, 8
decomposition = sm.tsa.seasonal_decompose(data.Column, model='additive', freq=12, extrapolate_trend=12)
fig = decomposition.plot()
plt.show()
Answered By: Anayar0401