首頁  >  文章  >  後端開發  >  使用 Pandas Datareader 存取雅虎財經資料時如何解決“TypeError:字串索引必須是整數”錯誤?

使用 Pandas Datareader 存取雅虎財經資料時如何解決“TypeError:字串索引必須是整數”錯誤?

Linda Hamilton
Linda Hamilton原創
2024-10-21 15:38:29759瀏覽

How to Resolve the

使用Pandas Datareader 存取雅虎財經資料時出現TypeError

使用Pandas Datareader 從Yahoo Finance 擷取股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會遇到股票資料時,使用者可能會與“TypeError”:字串索引必須是整數”錯誤。當“symbols”參數需要字串列表但遇到字串時,可能會出現此問題。

要解決此錯誤,請確保“symbols 」參數是股票識別碼清單。以下是工作代碼的範例:

<code class="python">import pandas_datareader

end = "2022-12-15"
start = "2022-12-15"
stock_list = ["TATAELXSI.NS"]

data = pandas_datareader.get_data_yahoo(symbols=stock_list, start=start, end=end)

print(data)</code>

此外,名為raphi6 的GitHub 用戶提供了一個拉取請求,其中修復了此問題。要安裝此修復程序,請依照以下步驟操作:

  1. 安裝以下相依性:

    conda install pycryptodome pycryptodomex
  2. 卸載目前版本的Pandas Datareader:

    conda uninstall pandas-datareader
  3. 卸載目前版本的Pandas Datareader:
  4. pip install git+https://github.com/raphi6/pandas-datareader.git@ea66d6b981554f9d0262038aef2106dda7138316
安裝Pandas Datareader 的拉取請求版本:

<code class="python">import pandas_datareader as pdr
import pandas as pd

end = "2022-12-15"
start = "2022-12-15"
stock_list = ["TATAELXSI.NS"]

stock_symbol = stock_list[0]
stock_obj = pdr.DataReader(stock_symbol, 'yahoo', start, end)
stock_data = pd.DataFrame({stock_symbol: stock_obj['Close']})
</code>
或者,名為Nikhil Mulley 的用戶建議了一種解決方法,涉及pdr_override() 函數。

以上是使用 Pandas Datareader 存取雅虎財經資料時如何解決“TypeError:字串索引必須是整數”錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn