-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay_table.py
More file actions
156 lines (131 loc) · 5.07 KB
/
Copy pathdisplay_table.py
File metadata and controls
156 lines (131 loc) · 5.07 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Format tables for human-readable display (thousands, currency, %, lease)."""
from __future__ import annotations
import math
import numpy as np
import pandas as pd
def format_currency(value: float | None) -> str:
if value is None:
return ""
if isinstance(value, float) and (math.isnan(value) or not math.isfinite(value)):
return ""
return f"{float(value):,.2f}"
def format_percent(value: float | None) -> str:
"""Format a 0–100 percent value (e.g. 5.25 from yield*100) with 2 decimals."""
if value is None:
return ""
if isinstance(value, float) and (math.isnan(value) or not math.isfinite(value)):
return ""
return f"{float(value):,.2f}%"
def format_ratio_as_percent(ratio: float | None) -> str:
"""`ratio` in [0,1] e.g. 0.0525 -> 5.25%."""
if ratio is None:
return ""
if isinstance(ratio, float) and (math.isnan(ratio) or not math.isfinite(ratio)):
return ""
return f"{float(ratio) * 100.0:,.2f}%"
def remaining_lease_text_from_years(y: float) -> str:
"""
Turn decimal years (e.g. 92.4167) into '92y 5m 0d' style, consistent with
parsing `years + months/12` in clean.py.
"""
if y is None or not isinstance(y, (int, float, np.floating)):
return ""
yf = float(y)
if not math.isfinite(yf) or np.isnan(yf):
return ""
y = yf
y_i = int(y)
m_float = (y - y_i) * 12.0
m_i = int(m_float)
# carry if rounding pushes months to 12
if m_i >= 12:
y_i += 1
m_i = 0
d_part = m_float - m_i
d_i = int(max(0, min(29, round(d_part * 30.4375)))) # ~mean month length
return f"{y_i}y {m_i}m {d_i}d"
def gross_yield_table_for_display(df: pd.DataFrame) -> pd.DataFrame:
"""Readable strings for Streamlit: gross yield as %, medians with commas."""
if df.empty:
return df
out = df.copy()
if "gross_yield_pct" in out.columns:
out["gross yield (%)"] = out["gross_yield_pct"].map(format_percent)
elif "gross_yield" in out.columns:
out["gross yield (%)"] = (out["gross_yield"] * 100.0).map(format_percent)
if "median_resale" in out.columns:
out["median resale ($)"] = out["median_resale"].map(format_currency)
if "median_rent" in out.columns:
out["median rent ($/m)"] = out["median_rent"].map(format_currency)
if "n_resale" in out.columns:
out["n resale"] = out["n_resale"].map(lambda v: f"{int(v):,}" if pd.notna(v) else "")
_drop = (
"gross_yield",
"gross_yield_pct",
"median_resale",
"median_rent",
"n_resale",
)
out = out.drop(
columns=[c for c in _drop if c in out.columns],
errors="ignore",
)
front = [c for c in ("quarter", "town", "flat_type") if c in out.columns]
rest = [c for c in out.columns if c not in front]
return out[front + rest]
def correlation_edges_for_display(df: pd.DataFrame) -> pd.DataFrame:
if df.empty or "abs_correlation" not in df.columns:
return df
out = df.copy()
if "correlation" in out.columns:
out["ρ"] = out["correlation"].map(
lambda v: f"{float(v):,.4f}" if pd.notna(v) and np.isfinite(v) else ""
)
out = out.drop(columns=["correlation"], errors="ignore")
out["|ρ|"] = out["abs_correlation"].map(
lambda v: f"{float(v):,.4f}" if pd.notna(v) and np.isfinite(v) else ""
)
return out.drop(columns=["abs_correlation"], errors="ignore")
def storey_table_for_display(df: pd.DataFrame) -> pd.DataFrame:
if df.empty:
return df
out = df.copy()
if "median_resale" in out.columns:
out["median resale ($)"] = out["median_resale"].map(format_currency)
out = out.drop(columns=["median_resale"], errors="ignore")
return out
def block_table_for_display(df: pd.DataFrame) -> pd.DataFrame:
if df.empty:
return df
out = df.copy()
if "median_resale" in out.columns:
out["median resale ($)"] = out["median_resale"].map(format_currency)
out = out.drop(columns=["median_resale"], errors="ignore")
if "n_trans" in out.columns:
out["n trans"] = out["n_trans"].map(lambda v: f"{int(v):,}" if pd.notna(v) else "")
out = out.drop(columns=["n_trans"], errors="ignore")
return out
def cluster_medians_for_display(df: pd.DataFrame) -> pd.DataFrame:
if df.empty:
return df
out = df.copy()
for c in out.columns:
if c == "cluster_id":
continue
if out[c].dtype.kind in "fiu":
out[c] = out[c].map(
lambda v: f"{float(v):,.2f}" if pd.notna(v) and np.isfinite(float(v)) else ""
)
return out
def forecast_rmse_for_display(df: pd.DataFrame) -> pd.DataFrame:
if df.empty or "backtest_rmse" not in df.columns:
return df
out = df.copy()
def _fmt_rmse(v: object) -> str:
if not pd.notna(v) or not np.isfinite(float(v)):
return ""
return f"{float(v):,.2f}"
out["backtest RMSE"] = out["backtest_rmse"].map(_fmt_rmse)
if "town" in out.columns:
return out.drop(columns=["backtest_rmse"], errors="ignore")
return out