Skip to content

pycensus.accessors

Per-source GeoDataFrame accessors: gdf.lodes[...], gdf.acs[...], etc.

Technical aspects

Each source registers a pandas.api.extensions.register_dataframe_accessor accessor named after its :class:~pycensus.schema.ColumnSchema prefix. GeoDataFrame subclasses DataFrame, so a DataFrame-level accessor works identically on gdf.<source> for any GeoDataFrame. The accessor is a thin namespace: indexing it with a logical column name (or list of names) returns a :class:ColumnView, a wrapper around the selected column(s) adding two derivations:

- ``.density``: value(s) divided by the geometry's area in square
  meters (or length in meters for line geometries), computed with
  :func:`geohierarchy.utils.area` / :func:`geohierarchy.utils.length`.
- ``.relative``: value(s) divided by the column registered as their
  ``relative_to`` total in the source's :class:`ColumnSchema`.

Registration is global (like geopandas' own .geometry), so the accessor works on any GeoDataFrame carrying that source's prefixed columns -- both a source loader's direct output and a GeoDataFrame pulled back out of a :class:geohierarchy.GeoHierarchy level.

ColumnView(data, gdf, schema)

Wraps a selected column (or columns) with .density / .relative derivations.

Duck-types the wrapped pandas object by delegating attribute access to it, so e.g. gdf.lodes["population"].sum() works directly on the underlying Series without going through .data.

Attributes:

Name Type Description
data

The selected raw values -- a Series for a single column, a DataFrame for several.

Initialize the view.

Parameters:

Name Type Description Default
data Union[Series, DataFrame]

Selected column(s) from gdf.

required
gdf GeoDataFrame

Parent GeoDataFrame, used to compute .density.

required
schema ColumnSchema

Source's column schema, used to resolve .relative.

required

density property

Value(s) divided by geometry area in m2 (length in m for lines).

relative property

Value(s) divided by each column's registered relative_to total.

register_source_accessor(schema)

Register a GeoDataFrame accessor named schema.prefix.

Parameters:

Name Type Description Default
schema ColumnSchema

Column schema whose prefix becomes the accessor name (e.g. gdf.lodes for a schema with prefix="lodes").

required

Returns:

Type Description
type

The registered accessor class.