Добавление numpy массивов в словаре к фрейму данных - PullRequest
0 голосов
/ 18 марта 2020

Я пытаюсь сравнить результаты двух фильтров временных рядов. Одним из них является Ходрик-Прескотт. Я успешно использую этот код в этом фрагменте кода

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
plt.style.use(['seaborn-paper'])
import quantecon as qe
import statsmodels.api as sm
from statsmodels.tsa.api import VAR
from sklearn.preprocessing import PolynomialFeatures 
from statsmodels.tsa.base.datetools import dates_from_str
import datetime as dt

hp1 = {}
for reg in regions_only:
    for var in variables_some1:
        x = seasonal[f'{var}sa_{reg}'].seasadj
        temp_cycle, temp_trend = sm.tsa.filters.hpfilter(np.log(x).dropna(), lamb = 1600)
        hp1[f'{var}sa_{reg}_c'] = temp_cycle

for var in variables_some1:
    x = seasonal[f'{var}sa_espana'].seasadj
    temp_cycle, temp_trend = sm.tsa.filters.hpfilter(np.log(x).dropna(), lamb = 1600)
    hp1[f'{var}sa_espana_c'] = temp_cycle

# This seasonally adjusts all variables available to some countries       
hp2 = {}
for reg in regions_only_some:
    for var in variables_some2:
        x = seasonal[f'{var}sa_{reg}'].seasadj
        temp_cycle, temp_trend = sm.tsa.filters.hpfilter(np.log(x).dropna(), lamb = 1600)
        hp2[f'{var}sa_{reg}_c'] = temp_cycle

for var in variables_some2:
    x = seasonal[f'{var}sa_espana'].seasadj
    temp_cycle, temp_trend = sm.tsa.filters.hpfilter(np.log(x).dropna(), lamb = 1600)
    hp2[f'{var}sa_espana_c'] = temp_cycle

hp_ir = {}
for reg in regions_only:
    x = seasonal[f'irsa_{reg}']
    temp_cycle, temp_trend = sm.tsa.filters.hpfilter(x.dropna(), lamb = 1600)
    hp_ir[f'irsa_{reg}_c'] = temp_cycle    

hp_filtered = {**hp1, **hp2, **hp_ir}  

# We transform our dictionary onto a dataframe for ease of manipulation
hp_filtered = pd.DataFrame.from_dict(hp_filtered)

, в результате чего получается фрейм данных, который я могу графически изучить позже. Я пытаюсь сделать то же самое с аналогичным кодом

import xlsxwriter
import quantecon as qe
hp1h = {}
for reg in regions_only:
    for var in variables_some1:
        x = seasonal[f'{var}sa_{reg}'].seasadj
        temp_cycle, temp_trend = qe.hamilton_filter(np.log(x), 8,4)
        hp1h[f'{var}sa_{reg}_c'] = temp_cycle

for var in variables_some1:
    x = seasonal[f'{var}sa_espana'].seasadj
    temp_cycle, temp_trend = qe.hamilton_filter(np.log(x), 8,4)
    hp1h[f'{var}sa_espana_c'] = temp_cycle

# This seasonally adjusts all variables available to some countries       
hp2h = {}
for reg in regions_only_some:
    for var in variables_some2:
        x = seasonal[f'{var}sa_{reg}'].seasadj
        temp_cycle, temp_trend = qe.hamilton_filter(np.log(x), 8,4)
        hp2[f'{var}sa_{reg}_c'] = temp_cycle

for var in variables_some2:
    x = seasonal[f'{var}sa_espana'].seasadj
    temp_cycle, temp_trend = qe.hamilton_filter(np.log(x), 8,4)
    hp2h[f'{var}sa_espana_c'] = temp_cycle


hp_irh = {}
for reg in regions_only:
    x = seasonal[f'irsa_{reg}']
    temp_cycle, temp_trend = qe.hamilton_filter(x, 8,4)
    hp_irh[f'irsa_{reg}_c'] = temp_cycle  

hp_datah = {**hp1h, **hp2h, **hp_irh}  

Я успешно создаю словарь массивов numpy, а не словарь фреймов данных, и я действительно изо всех сил пытаюсь выяснить, как объединить их для фильтрации словари. В частности, я не вижу, как прикрепить тот же индекс даты и времени, который был у меня в предыдущем словаре.

До сих пор мне удалось поместить словарь в файл Excel через

workbook = xlsxwriter.Workbook(path_to_data + 'HamiltonFiltered.xlsx')
worksheet = workbook.add_worksheet()

row = 0
col = 0
worksheet.write(0, 0, 'variable')
worksheet.write(0, 1, 'data')
for key in hp_datah.keys():
    row += 1
    worksheet.write(row, col, key)
    for item in hp_datah[key]:
        worksheet.write(row, 0, key)
        worksheet.write(row, col + 1, np.nan_to_num(item))
        row += 1

workbook.close()

, но это потребует много работы по редактированию позже.

Спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...