Skip to content

pycensus.latest_columns

Detect year-suffixed column families and keep the latest member (data driven, no hard-coded column list).

Stored parquet files keep every historic variant; the MAPS only show the latest: the last municipal / state / national election and the latest-period housing / rent / price / income / tourist / migration columns.

Rules (latest_column_filter): * election columns ([<source>_]elec_<scope>_<year><suffix>_<metric>): per scope (national / state / municipal) only the columns of the latest year are kept (a 2023leg / 2023council variant counts as year 2023); * every other column whose name carries a year token (2016, FY2026, 2025M08, cagr_2016_2025) belongs to a family = the name with the year token(s) removed; inside a family only the latest period is kept; the un-suffixed member of a family (e.g. rent_median next to rent_median_2016) is the CURRENT period and wins over every suffixed one. * columns with two or more year tokens (ranges / bins such as built_2011_a_2020_share) and years glued to letters (hhIncome20000To24999) are never dropped; * protect (iterable of regexes) names columns never dropped.

parse_period(col)

(family key, period) of a column name; period is None when the name has no year token. Period = tuple of (year*100+month) per token.

Parameters:

Name Type Description Default
col str

Column name.

required

election_parts(col)

(scope, year, variant) for an election column (elec_national_2023leg_left_share -> ("national", 2023, "leg")), else None.

Parameters:

Name Type Description Default
col str

Column name.

required

latest_column_filter(columns, protect=())

(kept columns in input order, {dropped column: reason}).

Parameters:

Name Type Description Default
columns Iterable[str]

Column names.

required
protect Sequence[str]

Regexes of columns never dropped.

()

minor_party_columns(frame, max_parties=4, min_share=0.05)

Party columns to drop, judged on frame (the finest one).

Majority is judged nationwide AND within every region (state / autonomous community): a party is kept when it is among the max_parties most voted parties (above min_share of the party votes) of the whole country OR of any single region, so a party that wins one region but is small nationally stays. Every other ..._party_<name>_(votes|share) column is minor.

Parameters:

Name Type Description Default
frame

Finest frame.

required
max_parties int

Parties kept per area.

4
min_share float

Smallest share kept.

0.05

drop_historic_columns(df, protect=(), report=None)

df without the historic variants (a new frame; report["dropped"] gets the dropped names).

Parameters:

Name Type Description Default
df

Table.

required
protect Sequence[str]

Regexes of columns never dropped.

()
report Optional[dict]

Optional dict that receives the dropped names.

None

filter_map_frames(groups, protect=(), report_path=None)

Drop historic variants from every frame of groups ({"h3": {res: df}, "census": {level: df}, ...}, dicts modified in place).

Returns (and writes to report_path as JSON when given) {"<group>/<key>": {"dropped": {col: reason}}}; stored parquet files are untouched.

Parameters:

Name Type Description Default
groups Dict[str, dict]

Frames by group and key.

required
protect Sequence[str]

Regexes of columns never dropped.

()
report_path

Optional JSON report file.

None

filter_map_inputs(inputs, protect=(), report_path=None, add_globals=True)

build_city_map(**inputs)-shaped dict: add the period-free global columns, then drop historic variants (frames replaced in place). Returns the report {"<dict>/<key>": {"dropped": {...}}} (also written to report_path).

Parameters:

Name Type Description Default
inputs dict

build_city_map keyword arguments.

required
protect Sequence[str]

Regexes of columns never dropped.

()
report_path

Optional JSON report file.

None
add_globals bool

Add the global columns first.

True