-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathingest.py
More file actions
37 lines (29 loc) · 1.01 KB
/
Copy pathingest.py
File metadata and controls
37 lines (29 loc) · 1.01 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
32
33
34
35
36
37
"""Load raw HDB CSV into a DataFrame."""
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
import pandas as pd
from singapore_eda.constants import DEFAULT_RAW_CSV, HDB_CITATION_URL
@dataclass(frozen=True)
class IngestMeta:
source_path: Path
citation_url: str
n_rows: int
n_cols: int
def read_hdb_csv(
path: Path | str | None = None,
) -> tuple[pd.DataFrame, IngestMeta]:
"""Read HDB resale CSV. Column names are normalized to snake_case strings."""
path = Path(path) if path is not None else DEFAULT_RAW_CSV
if not path.exists():
msg = f"Raw data not found: {path}. Run: python -m singapore_eda or hdb-download"
raise FileNotFoundError(msg)
df = pd.read_csv(path)
df.columns = [c.strip().lower().replace(" ", "_") for c in df.columns]
meta = IngestMeta(
source_path=path.resolve(),
citation_url=HDB_CITATION_URL,
n_rows=int(len(df)),
n_cols=int(len(df.columns)),
)
return df, meta