0

I want to use a time series in python and calculate n forecasts. I tried to use a for cycle, but when I use n>=2 I get an error: "KeyError: 101"

I tried:

dateparse = lambda x: pd.datetime.strptime(x, '%YM%m')
df = pd.read_excel('test.csv', sheet_name=f'sheet_1', index_col=2, parse_dates=['date'], date_parser=dateparse)
ad = df['ad']
n = 2
k = 3
for x in range(n):
        tot = len(ad)-1
        adtf = 7 + 23*ad[tot-1] + 55*ad[tot-2] + 13*nu[tot-1] + 3*nu[tot-2]
        indexf = ad.index[tot]
        indexf += relativedelta(months=+1)
        i = pd.Index([indexf])
        ad = ad.append(pd.DataFrame({0:[adtf]}, index=i))
        nu = nu.append(pd.DataFrame({0:[k]}, index=i))
        print(ad)

PS: I have added nu = nu.append(pd.DataFrame({0:[k]}, index=i)) in order to have a value to use in the next cyle.

  • could you show what `df` looks like? see [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/6619250) – hongsy Jan 23 '20 at 13:59
  • df is like: `date,ad,nu 2010M01,12,23 2010M02,2,34` note that in the code I forgot to write `nu = df['nu']` and that I wrote .csv instead of .xlsx. – themagiulio Jan 24 '20 at 14:14

0 Answers0