Merging Time Series With Different Dates
Stock and bond markets in the U.S. are closed on different days. For example, although the bond market is closed on Columbus Day (around Oct 12) and Veterans Day (around Nov 11), the stock market is open on those days. One way to see the dates that the stock market is open and the bond market is closed is to convert both indexes of dates into sets and take the difference in sets.
The pandas .join()
method is a convenient tool to merge the stock and bond DataFrames on dates when both markets are open.
Stock prices and 10-year US Government bond yields, which were downloaded from FRED, are pre-loaded in DataFrames stocks
and bonds
.
This exercise is part of the course
Time Series Analysis in Python
Exercise instructions
- Convert the dates in the
stocks.index
andbonds.index
into sets. - Take the difference of the stock set minus the bond set to get those dates where the stock market has data but the bond market does not.
- Merge the two DataFrames into a new DataFrame,
stocks_and_bonds
using the.join()
method, which has the syntaxdf1.join(df2)
.- To get the intersection of dates, use the argument
how='inner'
.
- To get the intersection of dates, use the argument
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import pandas
import pandas as pd
# Convert the stock index and bond index into sets
set_stock_dates = set(stocks.index)
set_bond_dates = set(___)
# Take the difference between the sets and print
print(set_stock_dates - ___)
# Merge stocks and bonds DataFrames using join()
stocks_and_bonds = ___