import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
symbols = ["AAPL", "GLD", "TSLA", "GBL", "GOOGL"]
def compare_security(symbols):
start_date = "01-01-2019"
end_date = "01-12-2020"
dates = pd.date_range(start_date, end_date)
df1 = pd.DataFrame(index=dates)
df_SPY = pd.read_csv(
"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=SPY&apikey=XXXX&datatype=csv",
index_col="timestamp", usecols=["timestamp", "adjusted_close"], parse_dates=True, na_values=['nan'])
df_SPY = df_SPY.rename(columns={"adjusted_close": "SPY"})
df1 = df1.join(df_SPY, how="inner")
for symbol in symbols:
df_temp= pd.read_csv("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol={}&apikey=XXXX&datatype=csv".format(symbol),
index_col = "timestamp", usecols = ["timestamp", "adjusted_close"], parse_dates=True, na_values=['nan'])
df_temp = df_temp.rename(columns={"adjusted_close":symbol})
df1 = df1.join(df_temp)
return df1
def test_run():
df = compare_security(symbols)
print(df)
df.plot()
plt.title(symbols)
plt.show()
if __name__ == "__main__":
test_run()
它读取错误“ValueError:Usecols 与列不匹配,列预期但未找到:['timestamp','adjusted_close']”
但是,我检查了代码将检索的所有文件,它们都有各自的列。任何关于我哪里出错的澄清将不胜感激。