Intro to Data Visualization
  • Introduction
  • Getting started
    • Introduction to Pandas
    • Accessing Files on Colab
    • Reviewing Data
      • Understanding type(data) in Pandas
    • Data Types
      • Categorical Data
      • Numeric Data
      • Temporal Data
      • Geographic Data
    • How to Check Data Type
    • Slicing and Subsetting DataFrames
    • Aggregating Data
  • Visualization Types
    • Exploratory Process
    • Explanatory Process
  • data exploration
    • Exploration Overview
    • Exploration with Plotly
      • Exploring Distributions
      • Exploring Relationships
      • Exploring with Regression Plots
      • Exploring Correlations
      • Exploring Categories
      • Exploring Time Series
      • Exploring Stocks with Candlestick
      • Exploring with Facets
      • Exploring with Subplots
    • Exploring with AI
  • Data Explanation
    • Data Explanation with Plotly
      • Using Text
      • Using Annotations
      • Using Color
      • Using Shape
      • Accessibility
      • Using Animations
    • Use Cases
  • Exercises and examples
    • Stock Market
      • Loading Yahoo! Finance Data
      • Use Cases for YF
      • Exploring YF Data
      • Understanding Boeing Data Over Time
      • Polishing the visualization
      • Analyzing with AI
      • Comparisons
    • The Gapminder Dataset
      • Loading the Gapminder Data
      • Use Cases
      • Exploring the Data
      • Exporting a Static Image
Powered by GitBook
On this page
  1. Exercises and examples
  2. Stock Market

Loading Yahoo! Finance Data

The first requirement is to ensure that yfinance is installed. If using Google Colab, install the required libraries:

!pip install yfinance plotly pandas

After installing the yfinance library, the next step is to import the libraries and load the data. The yfinance library has the alias yf.

import yfinance as yf
import plotly.express as px
import pandas as pd

To fetch data from yfinance, choose the stock ticker and the date range. In this case, we are going to examine Boeing, a company with the ticker "BA", for the date range of January 1, 2023 to December 31, 2025. This will provide all the available data for 2025.

The download function has the parameters ticker, start, and end. Note that the format for the dates are "YYYY-MM-DD", so January 1, 2023 is written as "2023-01-01".

# Step 1: Fetch Boeing stock data
ticker = 'BA'  # Ticker symbol for Boeing
start_date = '2023-01-01'
end_date = '2025-12-31'

# Fetch historical data
boeing_data = yf.download(ticker, start=start_date, end=end_date)

# Step 2: Prepare the data
# Reset the index to make the 'Date' column accessible
boeing_data.reset_index(inplace=True)

By default, the yfinance returns the Date as a row name and not a column. The line reset_index(inplace=True) makes the Date a column. You must run only once and directly after you run yf.download().

PreviousStock MarketNextUse Cases for YF

Last updated 3 months ago