"""Build one table: every U.S. occupation, its AI exposure scores, and what happened to it. Inputs (all public, downloaded by research/download.sh into data/raw/): - BLS OEWS national estimates, May 2019-2025: employment and median pay per occupation - Eloundou et al. (2023) "GPTs are GPTs": predicted exposure per O*NET occupation - Anthropic Economic Index (Mar 2026): observed exposure per occupation, AI use per task - O*NET 30.0: occupation descriptions, tasks, alternate job titles Output: data/processed/occupations.csv and data/processed/tasks.csv """ from pathlib import Path import numpy as np import pandas as pd ROOT = Path(__file__).resolve().parent.parent RAW = ROOT / "data" / "raw" OUT = ROOT / "data" / "processed" YEARS = [2019, 2020, 2021, 2022, 2023, 2024, 2025] ONET = RAW / "db_30_0_text" def read_oews(year): """One row per detailed occupation: employment and median annual pay.""" df = pd.read_excel(RAW / f"oesm{year % 100}nat" / f"national_M{year}_dl.xlsx", dtype=str) df.columns = [c.upper() for c in df.columns] group = "O_GROUP" if "O_GROUP" in df.columns else "OCC_GROUP" df = df[df[group] == "detailed"] out = pd.DataFrame({"soc": df["OCC_CODE"], f"title_{year}": df["OCC_TITLE"]}) # "*" means not released and "#" means pay is above the top of the survey's range. # Both become missing numbers rather than guesses. for src, name in [("TOT_EMP", "emp"), ("A_MEDIAN", "wage")]: out[f"{name}_{year}"] = pd.to_numeric(df[src].str.replace(",", ""), errors="coerce") return out.set_index("soc") def read_predicted_exposure(): """Eloundou et al. score each O*NET occupation; several share one 6-digit SOC code, so average them.""" g = pd.read_csv(RAW / "gpts_occ.csv") g["soc"] = g["O*NET-SOC Code"].str[:7] cols = {"dv_rating_beta": "beta_gpt4", "human_rating_beta": "beta_human"} return g.groupby("soc")[list(cols)].mean().rename(columns=cols) def read_observed_exposure(): a = pd.read_csv(RAW / "job_exposure.csv", dtype={"occ_code": str}) return a.set_index("occ_code")["observed_exposure"].rename("observed") def read_onet_text(): """Description and search titles, taken from each SOC code's main O*NET profile (the .00 one).""" occ = pd.read_csv(ONET / "Occupation Data.txt", sep="\t", dtype=str) occ["soc"] = occ["O*NET-SOC Code"].str[:7] main = occ[occ["O*NET-SOC Code"].str.endswith(".00")].set_index("soc")["Description"] # Fall back to the first specialty profile when a code has no .00 profile. first = occ.groupby("soc")["Description"].first() desc = main.reindex(first.index).fillna(first).rename("description") # keep_default_na=False: O*NET writes "n/a" for "no short title", which pandas would otherwise turn into NaN. alt = pd.read_csv(ONET / "Alternate Titles.txt", sep="\t", dtype=str, keep_default_na=False) alt["soc"] = alt["O*NET-SOC Code"].str[:7] # Prefer short, common titles ("Barista", "Cashier") for the search box. alt["t"] = alt["Short Title"].where(alt["Short Title"] != "n/a", alt["Alternate Title"]) alt = alt[alt["t"].str.len() <= 40] # Titles reported by more sources are the ones people actually use ("Barista" before "Catering Barista"). alt["n_src"] = alt["Source(s)"].str.count(",") + 1 alt = alt.sort_values(["soc", "n_src", "t"], ascending=[True, False, True], key=lambda c: c.str.len() if c.name == "t" else c) titles = alt.groupby("soc")["t"].apply(lambda s: "|".join(dict.fromkeys(s))).rename("alt_titles") return pd.concat([desc, titles], axis=1) def read_tasks(): """Every O*NET task with Anthropic's share of it seen in real AI use (task penetration).""" tasks = pd.read_csv(ONET / "Task Statements.txt", sep="\t", dtype=str) tasks["soc"] = tasks["O*NET-SOC Code"].str[:7] pen = pd.read_csv(RAW / "task_penetration.csv") pen["key"] = pen["task"].str.strip().str.lower() pen = pen.drop_duplicates("key").set_index("key")["penetration"] tasks["penetration"] = tasks["Task"].str.strip().str.lower().map(pen) tasks = tasks.drop_duplicates(["soc", "Task"]) return tasks[["soc", "Task", "Task Type", "penetration"]].rename( columns={"Task": "task", "Task Type": "task_type"}) def main(): OUT.mkdir(parents=True, exist_ok=True) oews = [read_oews(y) for y in YEARS] df = oews[-1].join(oews[:-1], how="left") # 2025 occupations are the universe df = df.join(read_predicted_exposure()).join(read_observed_exposure()).join(read_onet_text()) df = df.rename(columns={"title_2025": "title"}).drop(columns=[f"title_{y}" for y in YEARS[:-1]]) df["major"] = df.index.str[:2] # Growth is measured in log points: ln(later / earlier). 0.10 is about +10%. df["g_post"] = np.log(df.emp_2025 / df.emp_2022) # after ChatGPT (Nov 2022) df["g_pre"] = np.log(df.emp_2022 / df.emp_2019) # before it: the placebo window df["g_covid"] = np.log(df.emp_2021 / df.emp_2019) # the pandemic drop df["w_post"] = np.log(df.wage_2025 / df.wage_2022) df["w_pre"] = np.log(df.wage_2022 / df.wage_2019) df.index.name = "soc" df.to_csv(OUT / "occupations.csv") read_tasks().to_csv(OUT / "tasks.csv", index=False) print(f"{len(df)} occupations; {df.beta_gpt4.notna().sum()} with predicted exposure; " f"{df.observed.notna().sum()} with observed exposure; {df.emp_2019.notna().sum()} with 2019 data") if __name__ == "__main__": main()