Shiller data in Python

Monthly U.S. stock prices, dividends, earnings, consumer prices, interest rates and CAPE since 1871, from Robert Shiller.
Published

November 27, 2026

Updated

November 29, 2026

Robert Shiller’s free Excel file behind Irrational Exuberance is on shillerdata.com. Cite Shiller. Run pip install pandas requests xlrd. The script saves one long-format CSV in FOLDER (default Downloads), overwriting any file of the same name.

If a script does not work as it should, email me.

Monthly stock market data

One row per month and variable from January 1871: the S&P Composite price, dividends and earnings, each before and after inflation, the consumer price index, the long-term interest rate, the total return price after inflation, and CAPE, the price over the ten-year average of earnings after inflation. Prices average daily closes, so the latest month can be partial. CAPE starts in January 1881, and the latest months have no dividends or earnings yet.

Choose these settings in the code:

  • COLUMNS, Shiller’s column numbers with your variable names. The header rows name all 22 columns, such as the total return CAPE in column 14.
  • FOLDER, the output folder.
import io
import re
from pathlib import Path

import pandas as pd
import requests

COLUMNS = {1: "price", 2: "dividend", 3: "earnings", 4: "cpi", 6: "long_rate", 7: "real_price",
           8: "real_dividend", 9: "real_total_return_price", 10: "real_earnings",
           12: "cape"}                  # Shiller's column numbers and your names (the file lists 22 columns)
FOLDER = Path.home() / "Downloads"   # change this to the folder for the CSV file

# Step 1. Find the link to Shiller's Excel file on his website and download the file into memory
page = requests.get("https://shillerdata.com/", timeout=60)
page.raise_for_status()
link = "https:" + re.search(r'href="(//[^"]*ie_data\.xls[^"]*)"', page.text).group(1)
response = requests.get(link, timeout=120)
response.raise_for_status()

# Step 2. The sheet "Data". Its first column is the month as a decimal, 1871.01 for January 1871 and 1871.1 for
# October 1871, and only rows with a month are data (the rows below the table are notes)
raw = pd.read_excel(io.BytesIO(response.content), sheet_name="Data", header=None, skiprows=8)
raw = raw[pd.to_numeric(raw[0], errors="coerce").notna()]
data = raw[list(COLUMNS)].rename(columns=COLUMNS).apply(pd.to_numeric, errors="coerce")
data.insert(0, "month", [f"{int(date)}-{round(date % 1 * 100):02d}" for date in raw[0].astype(float)])

# Step 3. Long format, one row per month and variable, without the empty values, saved as one CSV file
long = data.melt(id_vars="month", var_name="variable", value_name="value").dropna()
FOLDER.mkdir(parents=True, exist_ok=True)
long.to_csv(FOLDER / "shiller_monthly.csv", index=False)
print(len(long), "rows saved, from", data["month"].iloc[0], "to", data["month"].iloc[-1])
print(long.groupby("variable")["month"].agg(["min", "max", "count"]).to_string())

Full post: CAPE ratio backtest: time in the market vs timing the market since 1891