Give me feedback

ValueError: Usecols do not match names.

# build a dataframe in pandas
import pandas as pd

def test_run():
#define date range
start_date='2010-01-22'
end_date='2010-01-26'
dates=pd.date_range(start_date,end_date)

#create an empty dataframe
df1=pd.DataFrame(index=dates)

#read SPY data into temporary dataframe
dfSPY = pd.read_csv("C:\\Users\\Marco\\Desktop\\Machine Learning\\Udacity\\ML for Trading\\SPY.csv", index_col="Date",
parse_dates=True,usecols = ['Date','Adj Close'],
na_values=['nan'])

#rename 'Adj Close' column to 'SPY' to prevet clash
dfSPY=dfSPY.rename(columns={'Adj Close':'SPY'})

#join the two dataframes using DataFrame.join(), with how='inner'
df1=df1.join(dfSPY,how='inner')

#read in more stocks
symbols = ['BBAS3','USIM5','VALE5']
for symbol in symbols:
df_temp=pd.read_csv("C:\\Users\\Marco\\Desktop\\Machine Learning\\Udacity\\ML for Trading\\{}.csv".format(symbol), index_col='Date',
parse_dates=True,usecols=['Date','Adj Close']
,na_values=['nan'])
#rename to prevent clash
df_temp = df_temp.rename(columns={'Adj Close': symbol})
df1=df1.join(df_temp) #use default how='left'

print (df1)

###@ I try to run this code, and appear this error: "ValueError: Usecols do not match names."
###@I appreciate someone's help !
Thanks.

Reactions 0 reactions

No reactions yet, be the first: comment or tweak the code

Comment on this code:

Please Login or Register to leave a comment.