> ## 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.

# 3. Building a row

> Downloading and cleaning prices and pageviews into one row per stock per trading day.

The ETL (extract, transform, load) turns three outside sources into one clean table, `clean/master.parquet`: **one row per stock per trading day**, with that day's return, volume ratio, Wikipedia views and event name. Everything downstream reads only this table.

## The big idea

Each source gets a **download** script that saves raw files untouched (`raw/`, never committed) and a **clean** script that checks them and writes a Parquet table that matches its contract (`clean/`, small and committed so teammates can skip the downloads).

* **Prices** come from Yahoo Finance: adjusted close and volume for every retailer and SPY. Cleaning (with Spark) computes the daily return and the volume ratio (today's volume against its recent average).
* **Pageviews** come from the Wikimedia REST API, counting real users only (no bots). Renamed articles are added together per ticker and day.
* **Retail sales** come from the US Census via FRED. They are cleaned but not analysed (decision 0012).
* **Events** are not downloaded: they come from `config/` through `expand_events`.

`join_master.py` then joins prices, pageviews and events on ticker and date.

## Words you will meet

> [accordion] Parquet
>
> A column-oriented file format: compact, typed, and fast for Spark and pandas to read.

> [accordion] Adjusted close
>
> The closing price corrected for splits and dividends, so returns across those days are real returns.

> [accordion] Volume ratio
>
> A day's trading volume divided by its recent average. Above 1 means unusually busy; the model uses its logarithm.

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

```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
```

The [full diagram and its guide](https://calendar-effects.mintlify.site/diagrams/03-etl.md) explain every box.

### Read these files in this order

**1. etl/prices\_download.py**

Download daily prices for every in-scope retailer in config/retailers.csv, plus the market index (common.study.MARKET\_TICKER), from Yahoo Finance, over the study period. [Read it](https://calendar-effects.mintlify.site/reference/etl/prices_download.md)

**2. etl/prices\_clean.py**

Clean the raw price files with Spark and save clean/prices.parquet. [Read it](https://calendar-effects.mintlify.site/reference/etl/prices_clean.md)

**3. etl/pageviews\_download.py**

Download daily Wikipedia page views (real users only, no bots) for each article of an in-scope retailer in config/wiki\_articles.csv, over the study period, and save them to raw/wiki/\<article>.json. [Read it](https://calendar-effects.mintlify.site/reference/etl/pageviews_download.md)

**4. etl/pageviews\_clean.py**

Clean the raw page view files and save clean/pageviews.parquet. [Read it](https://calendar-effects.mintlify.site/reference/etl/pageviews_clean.md)

**5. etl/retail\_clean.py**

Clean the US Census monthly retail sales series listed in config/retail\_series.csv (downloaded from FRED) into one file. [Read it](https://calendar-effects.mintlify.site/reference/etl/retail_clean.md)

**6. etl/build\_events.py**

Build the `events` table (see docs/data-contracts.md) from config/event\_types.csv and config/event\_dates.csv, and write a copy to data/events.parquet for browsing. [Read it](https://calendar-effects.mintlify.site/reference/etl/build_events.md)

**7. etl/join\_master.py**

Join prices, page views and events into clean/master.parquet, the one table Kafka replays later. [Read it](https://calendar-effects.mintlify.site/reference/etl/join_master.md)

## Questions you should be able to answer

> [accordion] Why are raw files kept apart from clean ones?
>
> Raw files are exactly what the source sent, so cleaning can be re-run and changed without downloading again, and a mistake in cleaning never destroys the original.

> [accordion] Does SPY have Wikipedia views?
>
> No. The market index has no article, so its `views` are empty in master, and the streaming job only scores attention for retailers.

> [accordion] How do multi-day events appear in master?
>
> Prime Day spans several days, so `event_type` is set on every day from its start to its end date. The analysis still uses one day 0 per occurrence, from the calendar.

## Ideas to improve it

* Use the retail sales series: does a strong monthly sales report move retailers' CARs?
* Add news volume (for example GDELT) as a second attention measure.


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