Del diccionario
dicc_tickers = {"IBE.MC":"Iberdrola", "TEF.MC":"Telefonica", "R4.MC":"Renta4", "^IBEX":"Ibex35", "^GSPC":"S&P500",
"^IXIC":"Nasdaq", "^N225":"Nikkey_225", "^DJI":"DowJons", "^STOXX50E":"Eurostoxx50",
"IBE.MC":"Iberdrola", "IBE.MC":"Iberdrola", "IBE.MC":"Iberdrola",
'AAPL':"Apple", 'MSFT':"Microsoft", 'GOOG':"Google", "AMZN":"Amazon"}
Extraigo la lista
ticker_list = dicc_tickers.keys()
ticker_list
Salida:
dict_keys(['IBE.MC', 'TEF.MC', 'R4.MC', '^IBEX', '^GSPC', '^IXIC', '^N225', '^DJI', '^STOXX50E', 'AAPL', 'MSFT', 'GOOG', 'AMZN'])
Con el script siguiente, aplicándole ésta lista, importo las cotizaciones de todos los valores incluidos en ella.
import pandas as pd
import yfinance as yf
# Define the ticker list
tickers_list = ['IBE.MC', 'TEF.MC', 'R4.MC', '^IBEX', '^GSPC', '^IXIC', '^N225', '^DJI', '^STOXX50E', 'AAPL', 'MSFT', 'GOOG', 'AMZN']
# Download the data for the above tickers and extract the Adj Close column
price_data = yf.download(tickers_list, start="2019-01-02")['Adj Close']
# Set the index to a datetime object
price_data.index = pd.to_datetime(price_data.index)
# Display the first 5 rows
price_data.head()
Si lo intento utilizando directamente el diccionario para obtener esta lista, me da el error mencionado en el título.
import pandas as pd
import yfinance as yf
dicc_tickers = {"IBE.MC":"Iberdrola", "TEF.MC":"Telefonica", "R4.MC":"Renta4", "^IBEX":"Ibex35", "^GSPC":"S&P500",
"^IXIC":"Nasdaq", "^N225":"Nikkey_225", "^DJI":"DowJons", "^STOXX50E":"Eurostoxx50",
"IBE.MC":"Iberdrola", "IBE.MC":"Iberdrola", "IBE.MC":"Iberdrola",
'AAPL':"Apple", 'MSFT':"Microsoft", 'GOOG':"Google", "AMZN":"Amazon"}
# Define the ticker list
tickers_list = dicc_tickers.keys()
# Download the data for the above tickers and extract the Adj Close column
price_data = yf.download(tickers_list, start="2019-01-02")['Adj Close']
# Set the index to a datetime object
price_data.index = pd.to_datetime(price_data.index)
# Display the first 5 rows
price_data.head()
AttributeError Traceback (most recent call last) /tmp/ipykernel_3102/3050173474.py in 10 tickers_list = dicc_tickers.keys() 11 # Download the data for the above tickers and extract the Adj Close column ---> 12 price_data = yf.download(tickers_list, start="2019-01-02")['Adj Close'] 13 14 # Set the index to a datetime object
~/anaconda3/envs/yfinance/lib/python3.9/site-packages/yfinance/multi.py in download(tickers, start, end, actions, threads, group_by, auto_adjust, back_adjust, progress, period, show_errors, interval, prepost, proxy, rounding, timeout, **kwargs)
74 # create ticker list
75 tickers = tickers if isinstance(
---> 76 tickers, (list, set, tuple)) else tickers.replace(',', ' ').split()
77
78 # accept isin as ticker
AttributeError: 'dict_keys' object has no attribute 'replace'
¿Cuál puede ser la causa?. Agradeceré ayuda.
dicc_tickers.keys()
no es un diccionario; sólo son sus llaves.yfinance
que causa el error, parece que si no le pasas una lista, un conjunto o una tupla, asume que lo que le pasaste es una cadena. Pero es undict_keys
(un objeto que representa las llaves del diccionario). La solución es convertir el resultado dedicc_tickers.keys()
a tupla, lista o conjunto con los respectivos constructorestuple
,list
yset
. Ejemplo:list(dicc_tickers.keys())