Getting "TypeError: string indices must be integers" Error when Fetching Yahoo Finance Data with Pandas Datareader
When using Pandas Datareader to acquire stock data from Yahoo Finance, an error indicating "string indices must be integers" may occur. This occurs when attempting to access stock data using a string index rather than an integer.
Solution
Ensure that the symbols parameter in get_data_yahoo is passed as a list of strings rather than a single string. For instance, instead of symbols="TATAELXSI.NS", use symbols=["TATAELXSI.NS"].
Alternative Fix Using pdr_override() Method
An alternative workaround method includes using the pdr_override function to specify the stock symbol as a string:
<code class="python">import pandas_datareader.data as pdr symbols = ["TATAELXSI.NS"] with pdr.pdr_override(): data = pdr.get_data_yahoo(symbols=symbols, start=start, end=end)</code>
This method allows for using a string as the symbol argument by instructing the function to treat it as an integer.
Additional Notes
The above is the detailed content of How to Resolve \'TypeError: string indices must be integers\' Error when Retrieving Yahoo Finance Data with Pandas Datareader?. For more information, please follow other related articles on the PHP Chinese website!