> ## Documentation Index
> Fetch the complete documentation index at: https://calendar-effects.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ETL: Extract (download), Transform (clean), Load (save)

> docs/architecture/03-etl.mmd

```mermaid theme={null}
%% Calendar Effects · 3 of 8 · ETL: Extract (download), Transform (clean), Load (save).
%% Paste into Miro: Creation bar > Diagram > Build with code.
flowchart LR
  subgraph PRICES["Stock prices · Mahad"]
    yahoo(["Yahoo Finance<br/>via the yfinance library"])
    pdl["etl/prices_download.py<br/>39 tickers = 38 retailers + SPY<br/>daily, split-adjusted close<br/>skips files already downloaded"]
    praw[/"raw/prices/TICKER.csv<br/>never committed"/]
    pcl["etl/prices_clean.py · PySpark<br/>ret = today's price / yesterday's − 1<br/>vol_ratio = volume / average of the 20 days before<br/>a Window over each ticker, sorted by date"]
    prices[("clean/prices.parquet<br/>ticker, date, adj_close, volume, ret, vol_ratio<br/>98,046 rows = 39 × 2,514 trading days")]
  end

  subgraph VIEWS["Wikipedia attention · Sanchi"]
    wm(["Wikimedia Pageviews REST API<br/>real users only, no bots"])
    vdl["etl/pageviews_download.py<br/>daily views per article"]
    vraw[/"raw/wiki/ARTICLE.json"/]
    vcl["etl/pageviews_clean.py · PySpark + pandas<br/>adds renamed titles together<br/>e.g. Amazon.com + Amazon_(company)<br/>days with no data become 0"]
    views[("clean/pageviews.parquet<br/>article, ticker, date, views<br/>every day 2016–2025")]
  end

  subgraph RETAIL["Retail sales · Areej"]
    fred(["FRED, US Census data<br/>downloaded by hand"])
    rraw[/"raw/census/CODE.csv"/]
    rcl["etl/retail_clean.py · pandas<br/>mom_change = change from last month<br/>release_date ≈ 15th of next month"]
    retail[("clean/retail_sales.parquet<br/>category, month, sales, mom_change, release_date<br/>used by the charts, not streamed")]
  end

  subgraph EVENTS["Event calendar · Anushiya"]
    cal["config/event_types.csv + event_dates.csv"]
    exp["expand_events() in src/common/contracts.py<br/>turns rules into dates, e.g. Black Friday 2024 = 2024-11-29<br/>etl/build_events.py saves a copy only for browsing"]
  end

  join["etl/join_master.py · Sanchi<br/>start from prices, one row per stock per trading day<br/>+ views of that stock that day<br/>+ event_type if an event falls on that trading day<br/>+ country and retailer_type from config/retailers.csv<br/>checks the master contract before saving"]
  master[("clean/master.parquet<br/>ticker, date, adj_close, volume, ret, vol_ratio,<br/>views, event_type, country, retailer_type<br/>98,046 rows · this is what Kafka replays")]

  yahoo --> pdl --> praw --> pcl --> prices
  wm --> vdl --> vraw --> vcl --> views
  fred --> rraw --> rcl --> retail
  cal --> exp
  prices --> join
  views --> join
  exp --> join
  join --> master
```

## Guide

```mermaid theme={null}
%% Calendar Effects · Guide 3 · ETL: getting and cleaning the data · goes next to diagram 03-etl.mmd
%% Miro: paste onto a board, or Creation bar > Diagram > Build with code. Read top to bottom.
flowchart TB
  T["GUIDE 3 · ETL · GETTING AND CLEANING THE DATA<br/>etl/ · raw/ → clean/"]

  subgraph S1["1 · WHAT ETL MEANS"]
    direction TB
    a1["EXTRACT<br/>download"]
    a2["TRANSFORM<br/>clean into a fixed shape"]
    a3["LOAD<br/>save as a Parquet table"]
    a4["raw/ vs clean/<br/>raw is never committed<br/>clean is small and committed"]
  end

  subgraph S2["2 · STOCK PRICES · Mahad"]
    direction LR
    p1["YAHOO FINANCE<br/>38 retailers + SPY"] --> p2["prices_download.py<br/>split-adjusted closes"] --> p3["prices_clean.py<br/>ret · vol_ratio · PySpark"] --> p4[("prices.parquet<br/>98,046 rows")]
  end

  subgraph S3["3 · WIKIPEDIA PAGE VIEWS · Sanchi"]
    direction LR
    v1["WIKIMEDIA API<br/>daily views, no bots"] --> v2["pageviews_download.py<br/>one JSON per article"] --> v3["pageviews_clean.py<br/>renamed titles added<br/>missing days → 0"] --> v4[("pageviews.parquet<br/>views per ticker per day")]
  end

  subgraph S4["4 · RETAIL SALES · Areej"]
    direction LR
    r1["FRED<br/>6 US Census series"] --> r2["retail_clean.py<br/>monthly change<br/>release date ≈ the 15th"] --> r3[("retail_sales.parquet<br/>not used by the pipeline yet")]
  end

  subgraph S5["5 · THE MASTER JOIN · join_master.py · Sanchi"]
    direction LR
    m1["START FROM PRICES<br/>1 row per stock-day"] --> m2["+ VIEWS<br/>empty for SPY"] --> m3["+ EVENT<br/>from expand_events"] --> m4["+ LABELS<br/>country, retailer_type"] --> m5[("master.parquet<br/>98,046 rows<br/>what Kafka replays")]
  end

  subgraph S6["6 · WATCH OUT"]
    direction TB
    w1["NO US THANKSGIVING ROWS<br/>the market is closed that day"]
    w2["PRIME DAY<br/>labelled on each of its days"]
    w3["TWO EVENTS ON ONE DAY<br/>the join stops: rows would double"]
  end

  T --> S1 --> S2 --> S3 --> S4 --> S5 --> S6

  classDef head fill:#2C2C2A,stroke:#2C2C2A,color:#FFFFFF
  classDef idea fill:#E6F1FB,stroke:#185FA5,color:#0C447C
  classDef step fill:#E1F5EE,stroke:#0F6E56,color:#085041
  classDef file fill:#F1EFE8,stroke:#5F5E5A,color:#444441
  classDef win fill:#EAF3DE,stroke:#3B6D11,color:#27500A
  classDef warn fill:#FAECE7,stroke:#993C1D,color:#712B13
  class T head
  class a1,a2,a3,a4 idea
  class p1,v1,r1 file
  class p2,p3,v2,v3,r2,m1,m2,m3,m4 step
  class p4,v4,r3,m5 win
  class w1,w2,w3 warn
  style S1 fill:#FFFFFF,stroke:#B4B2A9
  style S2 fill:#FFFFFF,stroke:#B4B2A9
  style S3 fill:#FFFFFF,stroke:#B4B2A9
  style S4 fill:#FFFFFF,stroke:#B4B2A9
  style S5 fill:#FFFFFF,stroke:#B4B2A9
  style S6 fill:#FFFFFF,stroke:#B4B2A9
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.