When working with financial data in Python, the yfinance
library is a go-to tool for retrieving stock information. One of the key components of yfinance
is the Ticker
object, which holds all the data related to a specific stock or company. But how do we know what data and methods are available to us within a Ticker
object?
In this post, we’ll explore how to discover the attributes and methods that are available within the yfinance.Ticker
object, and how you can use them to access important stock information.
1. Discovering the attributes and methods with dir()
To start exploring what we can do with a yfinance.Ticker
object, we can use the built-in Python function dir()
. This function will give us a list of all the attributes and methods that belong to the Ticker
object.
Here’s how to use it:
import yfinance as yf
# Create a Ticker object for Apple stock (AAPL)
ticker = "AAPL"
stock = yf.Ticker(ticker)
# Use dir() to list the attributes and methods available in the Ticker object
print(dir(stock))
Running this will output a list of attributes and methods associated with the stock
object, including important ones like .info
, .history()
, .dividends
, and more.
Example:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_analysis', '_data', '_download_options', '_earnings', '_earnings_dates', '_expirations', '_fast_info', '_fetch_ticker_tz', '_financials', '_fundamentals', '_funds_data', '_get_ticker_tz', '_holders', '_isin', '_lazy_load_price_history', '_news', '_options2df', '_price_history', '_quote', '_shares', '_tz', '_underlying', 'actions', 'analyst_price_targets', 'balance_sheet', 'balancesheet', 'basic_info', 'calendar', 'capital_gains', 'cash_flow', 'cashflow', 'dividends', 'earnings', 'earnings_dates', 'earnings_estimate', 'earnings_history', 'eps_revisions', 'eps_trend', 'fast_info', 'financials', 'funds_data', 'get_actions', 'get_analyst_price_targets', 'get_balance_sheet', 'get_balancesheet', 'get_calendar', 'get_capital_gains', 'get_cash_flow', 'get_cashflow', 'get_dividends', 'get_earnings', 'get_earnings_dates', 'get_earnings_estimate', 'get_earnings_history', 'get_eps_revisions', 'get_eps_trend', 'get_fast_info', 'get_financials', 'get_funds_data', 'get_growth_estimates', 'get_history_metadata', 'get_income_stmt', 'get_incomestmt', 'get_info', 'get_insider_purchases', 'get_insider_roster_holders', 'get_insider_transactions', 'get_institutional_holders', 'get_isin', 'get_major_holders', 'get_mutualfund_holders', 'get_news', 'get_recommendations', 'get_recommendations_summary', 'get_revenue_estimate', 'get_sec_filings', 'get_shares', 'get_shares_full', 'get_splits', 'get_sustainability', 'get_upgrades_downgrades', 'growth_estimates', 'history', 'history_metadata', 'income_stmt', 'incomestmt', 'info', 'insider_purchases', 'insider_roster_holders', 'insider_transactions', 'institutional_holders', 'isin', 'major_holders', 'mutualfund_holders', 'news', 'option_chain', 'options', 'proxy', 'quarterly_balance_sheet', 'quarterly_balancesheet', 'quarterly_cash_flow', 'quarterly_cashflow', 'quarterly_earnings', 'quarterly_financials', 'quarterly_income_stmt', 'quarterly_incomestmt', 'recommendations', 'recommendations_summary', 'revenue_estimate', 'sec_filings', 'session', 'shares', 'splits', 'sustainability', 'ticker', 'upgrades_downgrades']
2. Key attributes and methods to explore
Now that we know how to get the list of available methods, let’s take a look at some of the most useful attributes and methods that you can access:
– .info
The .info
attribute provides a dictionary containing important details about the stock, such as the company’s name, market cap, P/E ratio, sector, and more.
Example:
print(stock.info)
{'address1': 'One Apple Park Way', 'city': 'Cupertino', 'state': 'CA', 'zip': '95014', 'country': 'United States', 'phone': '(408) 996-1010', 'website': 'https://www.apple.com', 'industry': 'Consumer Electronics', 'industryKey': 'consumer-electronics', 'industryDisp': 'Consumer Electronics', 'sector': 'Technology', 'sectorKey': 'technology', 'sectorDisp': 'Technology', 'longBusinessSummary': 'Apple Inc. designs, manufactures, and markets smartphones, personal computers, tablets, wearables, and accessories worldwide. The company offers iPhone, a line of smartphones; Mac, a line of personal computers; iPad, a line of multi-purpose tablets; and wearables, home, and accessories comprising AirPods, Apple TV, Apple Watch, Beats products, and HomePod. It also provides AppleCare support and cloud services; and operates various platforms, including the App Store that allow customers to discover and download applications and digital content, such as books, music, video, games, and podcasts, as well as advertising services include third-party licensing arrangements and its own advertising platforms. In addition, the company offers various subscription-based services, such as Apple Arcade, a game subscription service; Apple Fitness+, a personalized fitness service; Apple Music, which offers users a curated listening experience with on-demand radio stations; Apple News+, a subscription news and magazine service; Apple TV+, which offers exclusive original content; Apple Card, a co-branded credit card; and Apple Pay, a cashless payment service, as well as licenses its intellectual property. The company serves consumers, and small and mid-sized businesses; and the education, enterprise, and government markets. It distributes third-party applications for its products through the App Store. The company also sells its products through its retail and online stores, and direct sales force; and third-party cellular network carriers, wholesalers, retailers, and resellers. Apple Inc. was founded in 1976 and is headquartered in Cupertino, California.', 'fullTimeEmployees': 164000, 'companyOfficers': [{'maxAge': 1, 'name': 'Mr. Timothy D. Cook', 'age': 63, 'title': 'CEO & Director', 'yearBorn': 1961, 'fiscalYear': 2023, 'totalPay': 16239562, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Jeffrey E. Williams', 'age': 60, 'title': 'Chief Operating Officer', 'yearBorn': 1964, 'fiscalYear': 2023, 'totalPay': 4637585, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Ms. Katherine L. Adams', 'age': 60, 'title': 'Senior VP, General Counsel & Secretary', 'yearBorn': 1964, 'fiscalYear': 2023, 'totalPay': 4618064, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': "Ms. Deirdre O'Brien", 'age': 57, 'title': 'Chief People Officer & Senior VP of Retail', 'yearBorn': 1967, 'fiscalYear': 2023, 'totalPay': 4613369, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Kevan Parekh', 'age': 52, 'title': 'Senior VP & CFO', 'yearBorn': 1972, 'fiscalYear': 2023, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Chris Kondo', 'title': 'Senior Director of Corporate Accounting', 'fiscalYear': 2023, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Suhasini Chandramouli', 'title': 'Director of Investor Relations', 'fiscalYear': 2023, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Greg Joswiak', 'title': 'Senior Vice President of Worldwide Marketing', 'fiscalYear': 2023, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Adrian Perica', 'age': 50, 'title': 'Head of Corporate Development', 'yearBorn': 1974, 'fiscalYear': 2023, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Michael Fenger', 'title': 'VP of Worldwide Sales', 'fiscalYear': 2023, 'exercisedValue': 0, 'unexercisedValue': 0}], 'auditRisk': 3, 'boardRisk': 1, 'compensationRisk': 3, 'shareHolderRightsRisk': 1, 'overallRisk': 1, 'governanceEpochDate': 1735689600, 'compensationAsOfEpochDate': 1703980800, 'irWebsite': 'http://investor.apple.com/', 'maxAge': 86400, 'priceHint': 2, 'previousClose': 223.66, 'open': 224.975, 'dayLow': 221.41, 'dayHigh': 225.63, 'regularMarketPreviousClose': 223.66, 'regularMarketOpen': 224.975, 'regularMarketDayLow': 221.41, 'regularMarketDayHigh': 225.63, 'dividendRate': 1.0, 'dividendYield': 0.0045, 'exDividendDate': 1731024000, 'payoutRatio': 0.1612, 'fiveYearAvgDividendYield': 0.61, 'beta': 1.24, 'trailingPE': 36.64145, 'forwardPE': 26.867216, 'volume': 52621751, 'regularMarketVolume': 52621751, 'averageVolume': 47998746, 'averageVolume10days': 60790750, 'averageDailyVolume10Day': 60790750, 'bid': 213.52, 'ask': 236.11, 'bidSize': 100, 'askSize': 100, 'marketCap': 3350143172608, 'fiftyTwoWeekLow': 164.08, 'fiftyTwoWeekHigh': 260.1, 'priceToSalesTrailing12Months': 8.567374, 'fiftyDayAverage': 239.1426, 'twoHundredDayAverage': 218.0977, 'trailingAnnualDividendRate': 0.98, 'trailingAnnualDividendYield': 0.0043816506, 'currency': 'USD', 'enterpriseValue': 3421391028224, 'profitMargins': 0.23971, 'floatShares': 15091184209, 'sharesOutstanding': 15037899776, 'sharesShort': 157008120, 'sharesShortPriorMonth': 154104746, 'sharesShortPreviousMonthDate': 1732838400, 'dateShortInterest': 1735603200, 'sharesPercentSharesOut': 0.0104, 'heldPercentInsiders': 0.02066, 'heldPercentInstitutions': 0.62313, 'shortRatio': 3.37, 'shortPercentOfFloat': 0.0104, 'impliedSharesOutstanding': 15533599744, 'bookValue': 3.767, 'priceToBook': 59.1399, 'lastFiscalYearEnd': 1727481600, 'nextFiscalYearEnd': 1759017600, 'mostRecentQuarter': 1727481600, 'earningsQuarterlyGrowth': -0.358, 'netIncomeToCommon': 93736001536, 'trailingEps': 6.08, 'forwardEps': 8.31, 'lastSplitFactor': '4:1', 'lastSplitDate': 1598832000, 'enterpriseToRevenue': 8.75, 'enterpriseToEbitda': 25.407, '52WeekChange': 0.16194654, 'SandP52WeekChange': 0.23809385, 'lastDividendValue': 0.25, 'lastDividendDate': 1731024000, 'exchange': 'NMS', 'quoteType': 'EQUITY', 'symbol': 'AAPL', 'underlyingSymbol': 'AAPL', 'shortName': 'Apple Inc.', 'longName': 'Apple Inc.', 'firstTradeDateEpochUtc': 345479400, 'timeZoneFullName': 'America/New_York', 'timeZoneShortName': 'EST', 'uuid': '8b10e4ae-9eeb-3684-921a-9ab27e4d87aa', 'messageBoardId': 'finmb_24937', 'gmtOffSetMilliseconds': -18000000, 'currentPrice': 222.78, 'targetHighPrice': 325.0, 'targetLowPrice': 183.0, 'targetMeanPrice': 245.89659, 'targetMedianPrice': 250.0, 'recommendationMean': 1.95652, 'recommendationKey': 'buy', 'numberOfAnalystOpinions': 41, 'totalCash': 65171001344, 'totalCashPerShare': 4.311, 'ebitda': 134660997120, 'totalDebt': 119058997248, 'quickRatio': 0.745, 'currentRatio': 0.867, 'totalRevenue': 391034994688, 'debtToEquity': 209.059, 'revenuePerShare': 25.485, 'returnOnAssets': 0.21464, 'returnOnEquity': 1.5741299, 'grossProfits': 180682997760, 'freeCashflow': 110846001152, 'operatingCashflow': 118254002176, 'earningsGrowth': -0.341, 'revenueGrowth': 0.061, 'grossMargins': 0.46206, 'ebitdaMargins': 0.34437, 'operatingMargins': 0.31171, 'financialCurrency': 'USD', 'trailingPegRatio': 2.0302}
This will return a dictionary with a variety of details about the company.
– .dividends
The .dividends
method shows the dividend payouts for the stock over time. This is a useful way to track any cash returns to shareholders.
Example:
print(stock.dividends)
Date
1987-05-11 00:00:00-04:00 0.000536
1987-08-10 00:00:00-04:00 0.000536
1987-11-17 00:00:00-05:00 0.000714
1988-02-12 00:00:00-05:00 0.000714
1988-05-16 00:00:00-04:00 0.000714
...
2023-11-10 00:00:00-05:00 0.240000
2024-02-09 00:00:00-05:00 0.240000
2024-05-10 00:00:00-04:00 0.250000
2024-08-12 00:00:00-04:00 0.250000
2024-11-08 00:00:00-05:00 0.250000
Name: Dividends, Length: 85, dtype: float64
This will return a DataFrame with the dividend payouts, including the dates and amounts.
– .splits
The .splits
method shows the stock splits for the company, which is useful for understanding when a company decides to divide its shares, often to make them more accessible to retail investors.
Example:
print(stock.splits)
1987-06-16 00:00:00-04:00 2.0
2000-06-21 00:00:00-04:00 2.0
2005-02-28 00:00:00-05:00 2.0
2014-06-09 00:00:00-04:00 7.0
2020-08-31 00:00:00-04:00 4.0
Name: Stock Splits, dtype: float64
This will return a DataFrame with the stock splits, including dates and split ratios.
– .history()
The .history()
method allows you to retrieve historical market data, including stock prices, volume, dividends, and stock splits for a specific time period.
Example:
historical_data = stock.history(period="5d")
print(historical_data)
Open High Low Close Volume Dividends Stock Splits
Date
2025-01-17 00:00:00-05:00 232.119995 232.289993 228.479996 229.979996 68488300 0.0 0.0
2025-01-21 00:00:00-05:00 224.000000 224.419998 219.380005 222.639999 98070400 0.0 0.0
2025-01-22 00:00:00-05:00 219.789993 224.119995 219.789993 223.830002 64126500 0.0 0.0
2025-01-23 00:00:00-05:00 224.740005 227.029999 222.300003 223.660004 60234800 0.0 0.0
2025-01-24 00:00:00-05:00 224.779999 225.630005 221.410004 222.779999 54619500 0.0 0.0
This will return a DataFrame containing the stock price data for the last 5 days.
3. Why is this important?
The yfinance.Ticker
object is a powerful tool for accessing financial data, but you need to understand what’s available to make the most of it. By using dir()
, help()
, and exploring attributes like .info
, .dividends
, .splits
, and .history()
, you can uncover valuable insights about any stock you’re interested in.
That was it for this post!
Let me know what you want to see in the next posts in the comments.