# Chapter 9: pandas Fundamentals
# Fundamentals of Python for Financial Econometrics - pandas Fundamentals
# Dataset: Ceteris Lab teaching sample

# pandas Fundamentals
#
# **Opening question:** How can a rectangular dataset preserve labels, dates, and missing values while supporting fast analysis?

# %% Cell 2
import pandas as pd

df = pd.DataFrame({
    "province": ["ON", "QC", "BC", "ON"],
    "income": [62_000, 58_000, 65_000, 71_000],
    "employed": [True, True, False, True],
})
print(df.shape)
print(df.dtypes.astype(str).to_dict())

# **Interpretation check:** Interpretation. The structure reveals four observations and three variables with different data types.

# %% Cell 4
high_income = (
    df.loc[df["income"] >= 60_000]
      .assign(income_thousands=lambda x: x["income"] / 1_000)
      .sort_values("income", ascending=False)
)
print(high_income[["province", "income_thousands"]])

# **Interpretation check:** Interpretation. The chain reads as a sequence of analytical decisions and leaves the original DataFrame unchanged.

# Verified source output
#
# ```text
# (4, 3) {'province': 'object', 'income': 'int64', 'employed': 'bool'}
# ```
#
# ```text
# province income_thousands 3 ON 71.0 2 BC 65.0 0 ON 62.0
# ```
#
# ```text
# (4, 3)
# {'province': 'object', 'income': 'int64', 'employed': 'bool'}
# ```
#
# ```text
# province  income_thousands
# 3       ON              71.0
# 2       BC              65.0
# 0       ON              62.0
# ```
