amin mohammadi
amin mohammadi

Reputation: 1053

Pandas index returns value error: ValueError: Length mismatch: Expected axis has 199 elements, new values have 200 elements

I have written this piece of code but when I try use Series function

import pandas as pd
import numpy as np
import datetime as dt

# example values
dates = np.asarray(pd.date_range('2000-01-01', periods=200))

# create a dataframe
df = pd.DataFrame(np.random.randn(200, 4), index=dates, columns=['A', 'B', 'C', 'D'])

print(df)
start_idx = np.where(dates>=np.datetime64(dt.datetime.strptime('2000-01-01', "%Y-%m-%d")))[0][-1]

dateIndex = df[0: start_idx+1].index
print(dateIndex)


historydict = {}
for i in range(3):
    returnHistory = pd.Series(df[i+1:start_idx+2]['A'])
    returnHistory.index = dateIndex
    # the above two periods assigned to date t, the returns lagged by i periods
    rname="Hist"+str(i+1)
    historydict[rname]=returnHistory
hDataFrame = pd.DataFrame(historydict)
print(hDataFrame)

historydict will be used to keep track of series for t-1, t-2, t-3, each will become a column in a dataframe but

returnHistory.index = dateIndex

Gives this error:

ValueError: Length mismatch: Expected axis has 199 elements, new values have 200 elements

Upvotes: 1

Views: 1159

Answers (1)

kvorobiev
kvorobiev

Reputation: 5070

At line

returnHistory = pd.Series(temp)

is a pandas.DataFrame object with shape (199, 4). Try

returnHistory = pd.Series(temp['A'])

or other column.

Upvotes: 2

Related Questions