> ## 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/retail_clean.py

> Clean the US Census monthly retail sales series listed in config/retail_series.csv (downloaded from FRED) into one file.

`etl/retail_clean.py` · chapter [Building a row](https://calendar-effects.mintlify.site/tour/03-building-a-row.md), file 5 · 51 lines · [open in the repository](https://github.sfu.ca/aba395/Calendar-Effects/blob/main/etl/retail_clean.py)

## What it does

Clean the US Census monthly retail sales series listed in config/retail\_series.csv
(downloaded from FRED) into one file.

Input:  raw/census/\<CODE>.csv   (downloaded from fred.stlouisfed.org, never committed)
Output: clean/retail\_sales.parquet, the `retail_sales` contract
Columns: category, month, sales (millions of USD), mom\_change, release\_date

To add a series, add a row to config/retail\_series.csv and download its CSV.
Run from anywhere:  python3 etl/retail\_clean.py

## Connections

**Uses:** [`src/common/contracts.py`](https://calendar-effects.mintlify.site/reference/src/common/contracts), [`src/common/settings.py`](https://calendar-effects.mintlify.site/reference/src/common/settings), [`src/common/study.py`](https://calendar-effects.mintlify.site/reference/src/common/study)

## Source

```python title="etl/retail_clean.py" lines expandable theme={null}
"""Clean the US Census monthly retail sales series listed in config/retail_series.csv
(downloaded from FRED) into one file.

Input:  raw/census/<CODE>.csv   (downloaded from fred.stlouisfed.org, never committed)
Output: clean/retail_sales.parquet, the `retail_sales` contract
Columns: category, month, sales (millions of USD), mom_change, release_date

To add a series, add a row to config/retail_series.csv and download its CSV.
Run from anywhere:  python3 etl/retail_clean.py
"""
import pandas as pd

from common.contracts import load_retail_series, pandas_columns, require_contract
from common.settings import RAW_DIR, clean_path
from common.study import STUDY_END, STUDY_START

# FRED series code -> readable category name
series = {row['series_id']: row['category'] for row in load_retail_series()}

frames = []
for sid, cat in series.items():
    df = pd.read_csv(RAW_DIR / 'census' / (sid + '.csv'))
    df.columns = ['month', 'sales']     # FRED gives: observation_date, <CODE>
    df['month'] = pd.to_datetime(df['month'])
    # FRED writes missing values as '.', so turn anything non-numeric into NaN and drop it
    df['sales'] = pd.to_numeric(df['sales'], errors='coerce')
    df = df.dropna().sort_values('month')
    df['sales'] = df['sales'].astype('int64')  # a missing value made the column float; the contract wants whole numbers

    # Month-over-month change: (this month / last month) - 1.
    # Computed BEFORE cutting to the study period, so its first month compares
    # against the month before instead of being empty.
    df['mom_change'] = df['sales'].pct_change()

    df = df[(df['month'] >= pd.Timestamp(STUDY_START)) & (df['month'] <= pd.Timestamp(STUDY_END))].copy()
    df['category'] = cat

    # Census publishes about the middle of the next month (approximate).
    # e.g. November 2024 sales -> released around 2024-12-15.
    # This matters for "no look-ahead": a trader could only react after this date.
    df['release_date'] = df['month'] + pd.offsets.MonthBegin(1) + pd.Timedelta(days=14)
    frames.append(df)

out = pd.concat(frames)
for c in ['month', 'release_date']:
    out[c] = out[c].dt.date            # save as real dates, no time part
out = out[['category', 'month', 'sales', 'mom_change', 'release_date']]
require_contract('retail_sales', pandas_columns(out))
out.to_parquet(clean_path('retail_sales'), index=False)
print(len(out))
print(out.groupby('category').size())
```


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