Skip to content

pycensus.canonical

Canonical (global-schema) column naming for every source's load() output.

Technical aspects

census/global_schema.json is pyCensus' cross-country vocabulary: a list of feature_name entries (population, malePopulation, medianHouseholdIncome, ...) that mean the same thing in every country. Each country's schema.py already names its :class:~pycensus.schema.ColumnDef after the matching feature_name wherever a genuine equivalent exists (that is a stated convention in AGENT_GUIDE_NEW_CITY_OR_COUNTRY.md).

On the GeoDataFrame, however, every column used to carry the source prefix (inegi_population). :func:apply_canonical_names is the single shared step -- called by every source's load() right before it returns -- that drops the prefix from exactly those columns whose logical name is an exact global_schema.json feature, and leaves every source-specific column prefixed (inegi_populationCatholic). Callers can therefore read gdf["population"] in any country with no name matching of their own, while still seeing every extra column the census offers.

Nothing is ever dropped -- this is a pure rename.

On the population collision question: pyCensus never joins two different sources' GeoDataFrames onto one frame internally (each load() returns its own frame; worldwide.worldpop has no load() at all -- it returns raster paths), so bare canonical names cannot collide inside pyCensus. A consumer that merges a pyCensus frame onto a frame of its own that already has, say, a WorldPop-derived population column, must resolve that collision at its own merge point; it is not special-cased here.

global_feature_names() cached

Return the set of feature_name values in global_schema.json (cached).

is_canonical(name)

Whether a logical (unprefixed) column name is a global-schema feature.

Parameters:

Name Type Description Default
name str

Logical column name.

required

Returns:

Type Description
bool

True for a global feature.

canonical_rename_map(schema)

Return {prefixed_name: bare_name} for this schema's canonical columns.

Parameters:

Name Type Description Default
schema 'ColumnSchema'

A source's column schema.

required

apply_canonical_names(gdf, schema)

Rename a loader's output columns to bare global-schema names in place-ish.

Every column of gdf whose logical name (prefix stripped) is an exact global_schema.json feature_name is renamed to that bare name; all other columns keep their prefixed names. No column is ever dropped or reordered.

Parameters:

Name Type Description Default
gdf 'gpd.GeoDataFrame'

A source load()'s output GeoDataFrame, with prefixed columns.

required
schema 'ColumnSchema'

The source's :class:~pycensus.schema.ColumnSchema.

required

Returns:

Type Description
'gpd.GeoDataFrame'

The GeoDataFrame with canonical columns renamed (the same object

'gpd.GeoDataFrame'

when nothing needed renaming).

resolve_column(gdf, schema, name)

Return the actual column of gdf holding logical name, or None.

After :func:apply_canonical_names, a logical column may live on the frame either bare (canonical) or prefixed (source-specific), and a frame that skipped canonicalization has everything prefixed. This is the single place that decides which of the two is present.

Parameters:

Name Type Description Default
gdf

Any DataFrame/GeoDataFrame carrying this source's columns.

required
schema 'ColumnSchema'

The source's column schema.

required
name str

Logical or already-prefixed column name.

required

Returns:

Type Description
Optional[str]

The matching column label present on gdf, else None.