Skip to content

Importing Data

Once the DataFrame structure is understood, the next step is to bring external data into Python. In real projects, analysts rarely type observations in by hand. Instead, they import data from online databases, APIs, CSV files, Excel spreadsheets, or internal company systems.

This section introduces two common types of business data source. The first is macroeconomic and business-environment data from FRED (the Federal Reserve Economic Data database), useful for economics and business research. The second is individual stock data from Yahoo Finance, useful for business performance analysis, corporate case studies, and investment applications.

A note on terminology: the source material for this session referred to these generically as "external data source" and "external business data source." The worked code examples make clear these refer specifically to FRED (via the fredapi package) and Yahoo Finance (via the yfinance package), so the more specific names are used throughout below.

B.1 Importing Macroeconomic and Business-Environment Data from FRED

FRED provides economic and business time series such as interest rates, inflation, unemployment, GDP, and other business indicators. These variables are often used as background conditions in economic and business analysis. For example, an investment researcher might combine stock performance with interest-rate movements or broader macroeconomic indicators.

The important workflow to internalise here is: download the data, convert it into a DataFrame, reset the date index, rename columns clearly, and check that the imported data has the structure you expect.

Python Code Examples: Importing FRED Data

# pip install fredapi

import pandas as pd
from fredapi import Fred

# Use your own FRED API key (free to register for on the FRED website)
fred = Fred(api_key="YOUR_FRED_API_KEY")

# Download S&P 500 data
sp500_series = fred.get_series("SP500", "2023-11-01", "2023-12-31")

# Convert the FRED Series into a DataFrame
sp500 = pd.DataFrame(sp500_series, columns=["Price_SP500"])
sp500.reset_index(inplace=True)
sp500.rename(columns={"index": "Date"}, inplace=True)

# Clean and inspect
sp500.dropna(inplace=True)
sp500["Date"] = pd.to_datetime(sp500["Date"])

print(sp500.head())
print(sp500.info())

Expected output / what to inspect

Inspect the first rows, the Date column, the Price_SP500 value column, and the DataFrame information summary (.info()), which reports the row count, column names, data types, and how many non-missing values each column contains.

Why each step matters:

  • fred.get_series() downloads a single named time series (here, "SP500") between the two dates specified. FRED's series codes are its own internal identifiers, so it's worth searching the FRED website for the exact code of whichever indicator you actually need.
  • .reset_index(inplace=True) matters because FRED returns the series with dates as the index rather than as an ordinary column. Resetting the index turns those dates into a normal, workable column instead.
  • .rename(columns={"index": "Date"}) simply gives that new column a clearer name than the generic "index" pandas assigns by default.
  • pd.to_datetime() ensures the Date column is stored as a genuine datetime type, not just text that merely looks like a date. This distinction becomes essential the moment you try to sort chronologically, calculate a rolling average, or merge with another dataset by date, all covered in the sections that follow.

B.2 Importing Stock Data from Yahoo Finance

Stock data for individual firms is commonly used in investment analysis, event studies, risk analysis, and building broader financial datasets. A typical dataset might contain opening prices, high and low prices, closing prices, trading volume, dividends, and stock splits.

In many introductory applications, you won't need every available column. Selecting only the columns required for your analysis helps reduce unnecessary complexity and makes the resulting dataset considerably easier to understand at a glance.

Python Code Examples: Importing Yahoo Finance Data

# pip install yfinance

import pandas as pd
import yfinance as yf

# Download Microsoft stock data
msft = yf.Ticker("MSFT")
msft_df = msft.history(start="2023-11-01", end="2023-12-31")

# Inspect the available columns before deciding what to keep
print(msft_df.columns)

# Keep a small set of columns for analysis
msft_df = msft_df[["Close", "Volume"]].copy()
msft_df.reset_index(inplace=True)
msft_df.rename(columns={"Close": "Price_MSFT"}, inplace=True)

print(msft_df.head())
print(msft_df.tail())

Expected output / what to inspect

Inspect the available columns first, then confirm that only the variables actually needed for the analysis have been kept.

A few practical notes:

  • yf.Ticker("MSFT") creates a ticker object for Microsoft; .history() then downloads its historical price data over the specified date range.
  • .copy() after selecting columns is a small but important habit. Without it, msft_df would be a view onto the original data (similar to the NumPy slicing behaviour from Session 2), and later modifications could trigger a confusing SettingWithCopyWarning. Explicitly copying avoids this ambiguity entirely.
  • .reset_index(inplace=True) is needed here for the same reason as in the FRED example: Yahoo Finance returns the date as the DataFrame's index, and resetting it turns the date into an ordinary column, ready to be used as a merge key later in Section F.
  • Renaming "Close" to "Price_MSFT" at this stage, rather than later, is a good habit once you know you'll eventually be merging this dataset with others (such as the S&P 500 data above): giving each source's price column a distinct, source-specific name up front avoids naming collisions further down the workflow.