python-3、pandas datareader和yahoo错误remotedataerror:无法读取url

ecr0jaav  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(316)

我正在尝试使用yahoo finance导入股票数据我正在使用以下代码:

import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
plt.style.use("fivethirtyeight")
%matplotlib inline

# For reading stock data from yahoo

from pandas_datareader.data import DataReader

# For time stamps

from datetime import datetime

它运转良好。

from pandas_datareader import data as pdr

import yfinance as yf
yf.pdr_override() # <== that's all it takes :-)

# download dataframe

# The tech stocks we'll use for this analysis

tech_list = ['WIPRO.BO', 'INFY.BO', 'TCS.BO', 'HAPPSTMNDS.BO']

# Set up End and Start times for data grab

end = datetime.now()
start = datetime(end.year - 1, end.month, end.day)

# For loop for grabing yahoo finance data and setting as a dataframe

for stock in tech_list:   

# Set DataFrame as the Stock Ticker

      globals()[stock] = pdr.get_data_yahoo(stock, start, end)

运行下面提到的代码时,我遇到一个错误:

company_list = ['WIPRO.BO', 'INFY.BO', 'TCS.BO', 'HAPPSTMNDS.BO']
company_name = ["Wipro", "Infosys", "Tata_Consultancy_Services", "Happiest_Minds_Technologies"]

for company, com_name in zip(company_list, company_name):
    company["company_name"] = com_name

df = pd.concat(company_list, axis=0)
df.tail(10)

错误消息:

TypeError                                 Traceback (most recent call last)

<ipython-input-6-4753fcd8a7a3> in <module>
      3 
      4 for company, com_name in zip(company_list, company_name):
----> 5     company["company_name"] = com_name
      6 
      7 df = pd.concat(company_list, axis=0)

TypeError: 'str' object does not support item assignment

请帮我解决这个问题。
非常感谢^_^

wtlkbnrh

wtlkbnrh1#

arima用于预测单变量时间序列数据。不确定要预测的功能。下面是一个例子:(如果对你有效,请向上投票!)


# For loop for grabing yahoo finance data and setting as a dataframe

lt=[]
for stock in tech_list:   

# Set DataFrame as the Stock Ticker

  temp_df = pdr.get_data_yahoo(stock, start, end)
  temp_df = temp_df.reset_index()
  lt.append(temp_df)

# Each element in the list is a DataFrame

df = pd.concat([lt[0],lt[1],lt[2],lt[3]], axis=0)
df = df.reset_index(drop=True)
print(df.head())

Output:

        Date        Open        High        Low         Close       Adj Close   Volume
    0   2020-07-09  224.850006  224.850006  219.800003  221.600006  221.103027  198245
    1   2020-07-10  221.600006  223.449997  219.449997  222.000000  221.502121  109461
    2   2020-07-13  224.000000  229.000000  222.750000  227.550003  227.039673  385205
    3   2020-07-14  229.000000  231.600006  224.199997  225.050003  224.545288  449975
    4   2020-07-15  237.000000  265.500000  233.800003  262.950012  262.360291  6313161
mspsb9vt

mspsb9vt2#

fix_yahoo_财务包的名称已更改为yfinance。所以请试试这个代码。

from pandas_datareader import data as pdr

import yfinance as yf
yf.pdr_override() # <== that's all it takes :-)

# download dataframe

# The tech stocks we'll use for this analysis

tech_list = ['WIPRO.BO', 'INFY.BO', 'TCS.BO', 'HAPPSTMNDS.BO']

# Set up End and Start times for data grab

end = datetime.now()
start = datetime(end.year - 1, end.month, end.day)

# For loop for grabing yahoo finance data and setting as a dataframe

for stock in tech_list:   

# Set DataFrame as the Stock Ticker

      globals()[stock] = pdr.get_data_yahoo(stock, start, end)

get_data_yahoo()方法返回一个 Dataframe 。所以,根据您想要做的事情,您可以生成 Dataframe 列表并将列表连接在一起。

相关问题