Skip to content

pycensus.year_resolution

Resolve which vintage year(s) to fetch for a census source/level request.

Technical aspects

Per the schema redesign spec: "When doing a request on the census if no year specified then take the latest year for every source (latest year which provides the finest requested level). If a year is specified where the source is not available interpolate between two available years. year can be specified for the complete request or as a source:year dict."

This module is pure logic -- it knows nothing about HTTP fetching. A loader (e.g. :mod:pycensus.usa.acs5.loader) calls :func:resolve_year (single source) or :func:resolve_years_for_request (multiple sources, from a plain year or a {source: year} dict), gets back a :class:YearResolution describing either one concrete year to fetch, or two bracketing years to fetch-and-blend via :func:interpolate_frames.

YearResolution(mode, target_year, year=None, year_low=None, year_high=None) dataclass

How to satisfy one source's year request.

Attributes:

Name Type Description
mode str

"exact" if an available year matches directly (or was snapped to the nearest bound when the request fell outside the available range), "interpolate" if it must be linearly blended from two bracketing years.

year Optional[int]

The concrete year to fetch when mode == "exact".

year_low Optional[int]

Lower bracketing year when mode == "interpolate".

year_high Optional[int]

Upper bracketing year when mode == "interpolate".

target_year int

The originally requested (or resolved-latest) year -- equal to year when mode == "exact".

resolve_year(requested_year, available_years, level=None, min_year_by_level=None)

Resolve a single source's year request to a concrete fetch plan.

Parameters:

Name Type Description Default
requested_year Optional[int]

The year asked for, or None to mean "latest".

required
available_years Sequence[int]

All vintage years this source publishes at all (regardless of level).

required
level Optional[str]

Geometry level being requested, used to further restrict available_years via min_year_by_level.

None
min_year_by_level Optional[Dict[str, int]]

Per-level earliest year with data at that level, e.g. {"tract": 2009, "blockgroup": 2013, "block": None}. A None value means the level is never available.

None

Returns:

Name Type Description
A YearResolution

class:YearResolution describing an exact year to fetch, or two

YearResolution

bracketing years to interpolate between.

Raises:

Type Description
ValueError

If no year supports the requested level at all.

resolve_years_for_request(sources, year, available_years_by_source, level=None, min_year_by_level_by_source=None)

Resolve year requests for every source in one request.

Parameters:

Name Type Description Default
sources Sequence[str]

Source names being queried (e.g. ["acs5", "dhc"]).

required
year YearRequest

None (latest for every source), a single int (applied to every source), or a {source: year} dict (per-source, sources not present default to latest).

required
available_years_by_source Dict[str, Sequence[int]]

Per-source available vintage years.

required
level Optional[str]

Geometry level being requested.

None
min_year_by_level_by_source Optional[Dict[str, Dict[str, int]]]

Per-source min_year_by_level dict, see :func:resolve_year.

None

Returns:

Type Description
Dict[str, YearResolution]

{source: YearResolution} for every source in sources.

interpolate_frames(df_low, year_low, df_high, year_high, target_year, id_col, value_cols)

Linearly interpolate value columns between two years, per geography.

Applied after both years have been fetched independently (each row already summed/joined into its final logical columns). For each geography id present in both frames, every value column is blended as low + (high - low) * t where t = (target_year - year_low) / (year_high - year_low). A geography present in only one frame (e.g. a tract that didn't exist yet, or was split/merged between vintages) is dropped -- there's nothing sound to interpolate it from.

Parameters:

Name Type Description Default
df_low DataFrame

Data fetched at year_low.

required
year_low int

The earlier bracketing year.

required
df_high DataFrame

Data fetched at year_high.

required
year_high int

The later bracketing year.

required
target_year int

The year actually requested (year_low < target_year < year_high).

required
id_col str

Geography id column present in both frames, joined on.

required
value_cols Sequence[str]

Numeric columns to interpolate. Columns not listed here (other than id_col) are dropped from the result.

required

Returns:

Type Description
DataFrame

One row per geography id present in both inputs, with each of

DataFrame

value_cols linearly interpolated to target_year.

Raises:

Type Description
ValueError

If year_low, target_year, year_high aren't strictly increasing.