# Chapter 16: Obtaining Economic and Financial Data
# Fundamentals of Python for Financial Econometrics - Obtaining Economic and Financial Data
# Dataset: Ceteris Lab teaching sample

# Obtaining Economic and Financial Data
#
# **Opening question:** How can a live data source be used without making the analysis disappear when the network or provider changes?

# %% Cell 2
from io import StringIO
import pandas as pd

frozen_csv = StringIO("""date,FXUSDCAD
2024-04-15,1.3762
2024-04-16,1.3778
2024-04-17,1.3731
2024-04-18,1.3704
2024-04-19,1.3746
""")
fx = pd.read_csv(frozen_csv, parse_dates=["date"])
print(fx.shape[0], round(fx["FXUSDCAD"].mean(), 4))

# **Interpretation check:** Interpretation. The frozen file makes the example independent of a live request while preserving the official series identifier.

# %% Cell 4
source_url = "https://www.bankofcanada.ca/valet/observations/FXUSDCAD/json"
print("Official Bank of Canada endpoint:", source_url)
print("This browser lab uses the frozen five-observation sample above for reproducibility.")

# **Interpretation check:** Interpretation. The timeout and exception handling prevent an indefinite hang. Production code should also validate schema and use the frozen fallback.

# Verified source output
#
# ```text
# 23 1.3708
# ```
#
# ```text
# 5
# ```
