-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_append.py
More file actions
31 lines (24 loc) · 1.25 KB
/
load_append.py
File metadata and controls
31 lines (24 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import argparse
import duckdb
from get_latest_data import get_latest_data
def load_append(pull_date: str, db_path: str = 'cpi_database.duckdb'):
df = get_latest_data(pull_date)
with duckdb.connect(db_path) as con:
# Create table if it doesn't exist yet
con.execute("CREATE TABLE IF NOT EXISTS cpi_append (dates VARCHAR, cpi DOUBLE)")
# Find the maximum date currently in the database
max_date_result = con.execute("SELECT MAX(dates) FROM cpi_append").fetchone()[0]
# Filter the DataFrame to only include rows newer than max_date
if max_date_result is not None:
df = df[df['dates'] > max_date_result]
# Append the new rows
if not df.empty:
con.execute("INSERT INTO cpi_append SELECT * FROM df")
print(f"[Append] Appended {len(df)} new rows into cpi_append as of {pull_date}")
else:
print(f"[Append] No new rows to append as of {pull_date}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Load CPI data using Append method.')
parser.add_argument('--date', type=str, required=True, help='Pull date in YYYY-MM-DD format')
args = parser.parse_args()
load_append(args.date)