時系列データの分解

Pythonで学ぶ時系列データの可視化

Thomas Vincent

Head of Data Science, Getty Images

Python辞書

# Initialize a Python dictionnary
my_dict = {}

# Add a key and value to your dictionnary
my_dict['your_key'] = 'your_value'

# Add a second key and value to your dictionnary
my_dict['your_second_key'] = 'your_second_value'

# Print out your dictionnary
print(my_dict)
{'your_key': 'your_value',
 'your_second_key': 'your_second_value'}
Pythonで学ぶ時系列データの可視化

Python辞書を使った複数時系列の分解

# Import the statsmodel library
import statsmodels.api as sm
# Initialize a dictionary
my_dict = {}
# Extract the names of the time series
ts_names = df.columns
print(ts_names)
['ts1', 'ts2', 'ts3']
# Run time series decomposition
for ts in ts_names:
    ts_decomposition = sm.tsa.seasonal_decompose(jobs[ts])
    my_dict[ts] = ts_decomposition
Pythonで学ぶ時系列データの可視化

複数時系列の分解成分を抽出する

# Initialize a new dictionnary
my_dict_trend = {}
# Extract the trend component
for ts in ts_names:
    my_dict_trend[ts] = my_dict[ts].trend
# Convert to a DataFrame            
trend_df = pd.DataFrame.from_dict(my_dict_trend)
print(trend_df)
            ts1  ts2 ts3
datestamp                                                  
2000-01-01  2.2  1.3 3.6                            
2000-02-01  3.4  2.1 4.7                             
...
Pythonで学ぶ時系列データの可視化

Python辞書を活用しよう!

Pythonで学ぶ時系列データの可視化

Preparing Video For Download...