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

Viewed 7224

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();
1 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

Related