In this blog post I decompose a time series of monthly data using the pandas and statsmodels package in Python.
You can find the data that I use in this blog post in my github repo. It is a monthly average of daily car counts on different hubs on the Belgian highways.
I start of with importing the necessary Python packages and loading in the data. I also filter the the data to only contain traffic from one particular segment on the Belgian highways.
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
from pylab import rcParams
data = pd.read_csv('all_volume.csv')
data = data[data.identifier == 1092002419]
In the next lines of code I concatenate the year (jaar) and month (maand) column, I also add the first day of the month using the ‘-01’ substring. Finally, I convert the time series to a Timestamp.
data['datum'] = data['jaar'].astype(str) + '-' + data['maand'].astype(str) + '-01'
data['datum'] = pd.to_datetime(data['datum'], yearfirst = True)
data.head()
It is crucial that you convert your date column to a datetime, otherwise, when decomposing the time series later on, you will get the following error:
AttributeError: ‘Index’ object has no attribute ‘inferred_freq’
Finally, before the decomposition, I only select the datum and the voertuigen column and set the datum column as the index of the DataFrame.
data = data[['datum','voertuigen']]
data = data.set_index('datum')
Which brings us to our final goal, the decomposition of the time series into a seasonal component and a trend component.
rcParams['figure.figsize'] = 18, 8
decomposition = sm.tsa.seasonal_decompose(data, model='additive',freq=12, extrapolate_trend = 12)
fig = decomposition.plot()
plt.show()
In this case has, our time series index had no ‘freq’. Not setting it returns in the following error:
ValueError: You must specify a freq or x must be a pandas object with a timeseries index with a freq not set to None
Since I’m working with monthly data, setting the frequency to 12 seemed like a logical thing to do. To avoid NaN, I also set the extrapolate_trend parameters to 12.
Great success!
It was helpful. Thanks!
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://accounts.binance.com/ro/register-person?ref=UM6SMJM3
Your article gave me a lot of inspiration, I hope you can explain your point of view in more detail, because I have some doubts, thank you.
Pingback: seo site planner
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Your article helped me a lot, is there any more related content? Thanks!
Here is a vivid example of modern trends – the current structure of the organization is perfect for the implementation of thoughtful reasoning. It should be noted that the economic agenda of today is an interesting experiment to verify the priority requirements.
Given the key scenarios of behavior diluted by a fair amount of empathy, rational thinking unequivocally defines each participant as capable of making his own decisions regarding the progress of the professional community. Our business is not as unambiguous as it might seem: the new model of organizational activity unequivocally defines each participant as capable of making his own decisions regarding the timely implementation of the super -task.
Given the current international situation, promising planning is a qualitatively new stage of standard approaches. Of course, the existing theory directly depends on the distribution of internal reserves and resources.
For the modern world, the boundary of personnel training largely determines the importance of clustering efforts. The significance of these problems is so obvious that consultation with a wide asset clearly defines each participant as capable of making his own decisions regarding the economic feasibility of decisions.
There is something to think about: the actions of opposition representatives call us to new achievements, which, in turn, should be functionally spaced into independent elements. Suddenly, independent states are nothing more than the quintessence of the victory of marketing over the mind and should be indicated as applicants for the role of key factors.
Suddenly, the obvious signs of the victory of institutionalization to this day remain the destiny of liberals, who are eager to be verified in a timely manner. By the way, interactive prototypes form a global economic network and at the same time – are indicated as applicants for the role of key factors.
A variety of and rich experience tells us that the high -tech concept of public way requires an analysis of new proposals. However, one should not forget that the cohesion of the team of professionals plays a decisive importance for the personnel training system corresponding to the pressing needs.