-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeo_join.py
More file actions
59 lines (43 loc) · 1.87 KB
/
Copy pathgeo_join.py
File metadata and controls
59 lines (43 loc) · 1.87 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Join HDB rows to planning area, maturity, and connectivity reference tables."""
from __future__ import annotations
from pathlib import Path
import pandas as pd
from singapore_eda.paths import reference_dir
def load_town_to_planning_area(path: Path | None = None) -> pd.DataFrame:
p = path or (reference_dir() / "town_to_planning_area.csv")
df = pd.read_csv(p)
df["town"] = df["town"].astype(str).str.strip().str.upper()
return df
def load_town_maturity(path: Path | None = None) -> pd.DataFrame:
p = path or (reference_dir() / "town_maturity.csv")
df = pd.read_csv(p)
df["town"] = df["town"].astype(str).str.strip().str.upper()
return df.drop_duplicates(subset=["town"], keep="first")
def load_mrt_access(path: Path | None = None) -> pd.DataFrame:
p = path or (reference_dir() / "mrt_access_by_town.csv")
if not p.exists():
return pd.DataFrame(columns=["town", "mrt_station_count", "nearest_mrt_km_proxy"])
df = pd.read_csv(p)
df["town"] = df["town"].astype(str).str.strip().str.upper()
return df.drop_duplicates(subset=["town"], keep="first")
def enrich_with_reference(
df: pd.DataFrame,
*,
planning_path: Path | None = None,
maturity_path: Path | None = None,
mrt_path: Path | None = None,
) -> pd.DataFrame:
"""Add planning_area, region_ocr, maturity, mrt columns when town is present."""
out = df.copy()
if "town" not in out.columns:
return out
out["town"] = out["town"].astype(str).str.strip().str.upper()
pa = load_town_to_planning_area(planning_path)
out = out.merge(pa, on="town", how="left")
mat = load_town_maturity(maturity_path)
cols = mat[["town", "maturity", "notes"]]
out = out.merge(cols, on="town", how="left", suffixes=("", "_mat"))
mrt = load_mrt_access(mrt_path)
if not mrt.empty:
out = out.merge(mrt, on="town", how="left")
return out