1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python yfinance library

Discussão em 'Python' iniciado por Stack, Dezembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I have an Excel file with company names in column A. I want to use Python's yfinance library to populate the stock tickers into column B where they match column A. For testing purposes, I've limited my table to just one company name, Adobe, Inc.

    # Import libraries
    import pandas as pd
    !pip install yfinance
    import yfinance as yf
    import time

    # Import Existing Excel
    excel_file_path = 'MyFilePath/Company Ticker Report.xlsx'

    # Load Excel data
    excel_df = pd.read_excel(excel_file_path, sheet_name= 'MS LIst')


    def get_ticker(company_name):
    for CompanyNamein excel_df:
    try:
    ticker_dictionary = yf.Ticker(company_name)
    print(type(ticker_dictionary)) # confirm type
    print(ticker_dictionary) # test print dict
    related_tickers = []
    for row in ticker_dictionary['rows']:
    for news in row['news']:
    if 'relatedTickers' in news:
    related_tickers.append(news['relatedTickers']) # This should return the ticker for CompanyName
    time.sleep(1) # Pause execution of function for one second
    return related_tickers
    except Exception as e:
    print(f"An unexpected error occurred: {e}") # All errors
    return 'No Info'
    # Apply the function to the CompanyName column
    excel_df['Suggested Ticker'] = excel_df['CompanyName'].apply(get_ticker)
    # Save the updated data back to Excel
    excel_df.to_excel('Company Ticker Report.xlsx', index=False)
    display(excel_df) # test print


    When run my code, it tells me that the type for yf.Ticker is ticker, which is I think is the source of my problem (Here's the output: <class 'yfinance.ticker.Ticker'>). I then get this error message: An unexpected error occurred: 'Ticker' object is not subscriptable. I was able to convert ticker_dictionary to a dictionary with .dict, but then I could no longer refernce the nested containers inside of it that contained the data I needed (rows[news[relatedTickers]]]).

    Any suggestions on how to resolve this?

    When run my code, it tells me that the type for yf.Ticker is ticker, which is I think is the source of my problem (Here's the output: <class 'yfinance.ticker.Ticker'>). I then get this error message: An unexpected error occurred: 'Ticker' object is not subscriptable. I was able to convert ticker_dictionary to a dictionary with .dict, but then I could no longer refernce the nested containers inside of it that contained the data I needed (rows[news[relatedTickers]]]).

    Any suggestions on how to resolve this?

    Continue reading...

Compartilhe esta Página