mirror of
https://github.com/PR0M3TH3AN/python_charts.git
synced 2026-10-27 08:50:39 +00:00
Add argparse options to refresh_data
This commit is contained in:
1 parent
025821ed18
commit
4b3eeb21cc
3 files changed
+76
-14
No files matched your search
+30
-12
@@ -1,33 +1,51 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
refresh_data.py
|
||||
---------------
|
||||
Download UNRATE and DCOILWTICO from FRED and store them in SQLite
|
||||
(data/fred.db). Run on a connected machine; commit the DB afterwards.
|
||||
"""refresh_data.py
|
||||
-----------------
|
||||
Download one or more FRED series into ``data/fred.db``.
|
||||
|
||||
Run this script on a machine with internet access and commit the updated
|
||||
database afterwards.
|
||||
|
||||
Example:
|
||||
python scripts/refresh_data.py --series UNRATE CPIAUCSL --start 1960-01-01
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
import argparse
|
||||
import pathlib
|
||||
import sqlite3
|
||||
|
||||
import pandas as pd
|
||||
from pandas_datareader.data import DataReader
|
||||
import pandas as pd
|
||||
|
||||
HERE = pathlib.Path(__file__).resolve().parent
|
||||
DATA_PATH = HERE.parent / "data"
|
||||
DB_FILE = DATA_PATH / "fred.db"
|
||||
|
||||
START = "1948-01-01" # earliest UNRATE observation
|
||||
END = date.today().isoformat()
|
||||
SERIES = ("UNRATE", "DCOILWTICO")
|
||||
DEFAULT_START = "1948-01-01" # earliest UNRATE observation
|
||||
DEFAULT_END = date.today().isoformat()
|
||||
DEFAULT_SERIES = ("UNRATE", "DCOILWTICO")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
def main(argv: list[str] | None = None) -> None:
|
||||
p = argparse.ArgumentParser(description="Download FRED series to SQLite")
|
||||
p.add_argument(
|
||||
"--series",
|
||||
nargs="+",
|
||||
default=list(DEFAULT_SERIES),
|
||||
help="FRED series names",
|
||||
)
|
||||
p.add_argument("--start", type=str, default=DEFAULT_START, help="start date")
|
||||
p.add_argument("--end", type=str, default=DEFAULT_END, help="end date")
|
||||
args = p.parse_args(argv)
|
||||
|
||||
DATA_PATH.mkdir(exist_ok=True)
|
||||
with sqlite3.connect(DB_FILE) as conn:
|
||||
for s in SERIES:
|
||||
for s in args.series:
|
||||
print(f"↯ downloading {s} …")
|
||||
df = DataReader(s, "fred", START, END)
|
||||
df = DataReader(s, "fred", args.start, args.end)
|
||||
df.to_sql(s, conn, if_exists="replace", index_label="date")
|
||||
print(f"✓ wrote {s}: {len(df):,} rows")
|
||||
print(f"\nDone. SQLite DB at {DB_FILE} ({DB_FILE.stat().st_size/1024:.0f} KB)")
|
||||
|
||||
Reference in new issue
Block a user