获取“类型错误:字符串索引必须是整数”消息与Pandas DataReader

bpzcxfmw  于 2023-01-07  发布在  其他
关注(0)|答案(1)|浏览(256)

我正在使用DataReader从Pandas导入雅虎的财务数据,我想提一下,几周前我已经使用了这里第一张图片中的代码,没有任何问题。当我打开这个文件并试图运行它时,我得到了“类型错误:字符串索引必须为整数”消息。
1

# Load Packages
import numpy as np
import pandas as pd
import pandas_datareader.data as web
import matplotlib.pyplot as plt
%matplotlib inline 

# Read Data
test = web.DataReader(['TSLA','FB'], 'yahoo', start='2018/01/01', end='2019/12/31')
test.head()

Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_3456/2405099378.py in <module>
      1 # Read Data
----> 2 test = web.DataReader(['TSLA','FB'], 'yahoo', start='2018/01/01', end='2019/12/31')
      3 test.head()

c:\Users\jclay\anaconda3\envs\blockchain_dev\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    205                 else:
    206                     kwargs[new_arg_name] = new_arg_value
--> 207             return func(*args, **kwargs)
    208 
    209         return cast(F, wrapper)

c:\Users\jclay\anaconda3\envs\blockchain_dev\lib\site-packages\pandas_datareader\data.py in DataReader(name, data_source, start, end, retry_count, pause, session, api_key)
    376             retry_count=retry_count,
    377             pause=pause,
--> 378             session=session,
    379         ).read()
    380 

c:\Users\jclay\anaconda3\envs\blockchain_dev\lib\site-packages\pandas_datareader\base.py in read(self)
    256             df = self._dl_mult_symbols(self.symbols.index)
    257         else:
--> 258             df = self._dl_mult_symbols(self.symbols)
...
--> 153             data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
    154         except KeyError:
    155             msg = "No data fetched for symbol {} using {}"

TypeError: string indices must be integers

我甚至直接从panda的DataReader文档中复制粘贴了相同的代码,它也给出了相同的错误。一定是有什么东西更新了这个函数读取数据的方式,但是,我在文档中找不到变化。有其他人能够解决这个问题吗?
2

In [39]: import pandas_datareader.data as web

In [40]: import pandas as pd

In [41]: import datetime as dt

In [42]: df = web.DataReader('GE', 'yahoo', start='2019-09-10', end='2019-10-09')

In [43]: df.head()

Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_3456/472188857.py in <module>
----> 1 df = web.DataReader('GE', 'yahoo', start='2019-09-10', end='2019-10-09')
      2 
      3 df.head()

c:\Users\jclay\anaconda3\envs\blockchain_dev\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    205                 else:
    206                     kwargs[new_arg_name] = new_arg_value
--> 207             return func(*args, **kwargs)
    208 
    209         return cast(F, wrapper)

c:\Users\jclay\anaconda3\envs\blockchain_dev\lib\site-packages\pandas_datareader\data.py in DataReader(name, data_source, start, end, retry_count, pause, session, api_key)
    376             retry_count=retry_count,
    377             pause=pause,
--> 378             session=session,
    379         ).read()
    380 

c:\Users\jclay\anaconda3\envs\blockchain_dev\lib\site-packages\pandas_datareader\base.py in read(self)
    251         # If a single symbol, (e.g., 'GOOG')
    252         if isinstance(self.symbols, (string_types, int)):
--> 253             df = self._read_one_data(self.url, params=self._get_params(self.symbols))
...
--> 153             data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
    154         except KeyError:
    155             msg = "No data fetched for symbol {} using {}"

TypeError: string indices must be integers
vhmi4jdf

vhmi4jdf1#

我的代码也出现了同样的错误,雅虎财经改变了数据格式,导致DataReader无法获取所需的股票信息。我使用了yfinance,它与DataReader数据相同,只是这是一个免费库。你可以到here查看所有语法。

语法:

import yfinance as yf
df = yf.download('ticker_name', start, end)

您的密码:

df = web.DataReader('GE', 'yahoo', start='2019-09-10', end='2019-10-09')

改为:

import yfinance as yf 
df = yf.download('GE', start='2019-09-10', end='2019-10-09')

相关问题