1. Decompose time series data
In the previous exercises, you extracted some interesting patterns and seasonality from some of the time series in the jobs dataset. Remember in Chapter 3 how you were introduced to the concept of time-series decomposition, which allows you to automatically extract the seasonality, trend and noise of a time-series? In the following exercises, you will apply time series decomposition to the jobs data.
2. Python dictionaries
This case study will present you with the additional challenge of having to apply and visualize time series decomposition to multiple time series. For this, you will have to leverage the dictionary data structure in Python. It is best to think of a dictionary as an unordered set of keys paired to specific values, where the only requirement is that the keys are unique. As shown in line 1, a dictionary can be initialized with a pair of curly braces. Keys and associated values can then be added as shown in lines 2 and 3.
3. Decomposing multiple time series with Python dictionaries
By leveraging a key-value data structure such as Python dictionaries, you can store the results of each time series decomposition. In the example shown here, you begin by initializing a my_dict dictionary and extract the column names of the DataFrame df. This DataFrame contains multiple time series called ts1, ts2 and ts3, which can be extracted using the command in line 3. Finally, you can use a "for" loop to iterate through the columns of df and apply the seasonal_decompose() function from the statsmodels library, which are stored in my_dict.
4. Extract decomposition components of multiple time series
You can take things further and leverage the my_dict dictionary to extract specific components of each time series. As an example, the first line initializes a new my_dict_trend Python dictionary. The second line iterates through the contents of the my_dict dictionary and extracts their inferred trend, which are placed into the my_dict_trend dictionary. Then you use the pd dot DataFrame dot from_dict function from the Pandas library, which can be used to automatically convert the data in your dictionary into a DataFrame called trend_df. In this case, the "keys" of the jobs_trend dictionary will become the columns of the trend_df DataFrame, with the associated values populating the contents of the DataFrame.
5. Python dictionaries for the win!
Let's use Python dictionaries and time series decomposition on the jobs dataset!