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
|
|
year |
Optional[int]
|
The concrete year to fetch when |
year_low |
Optional[int]
|
Lower bracketing year when |
year_high |
Optional[int]
|
Upper bracketing year when |
target_year |
int
|
The originally requested (or resolved-latest) year --
equal to |
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 |
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
|
None
|
min_year_by_level
|
Optional[Dict[str, int]]
|
Per-level earliest year with data at that level,
e.g. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
YearResolution
|
class: |
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. |
required |
year
|
YearRequest
|
|
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 |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, YearResolution]
|
|
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 |
required |
year_low
|
int
|
The earlier bracketing year. |
required |
df_high
|
DataFrame
|
Data fetched at |
required |
year_high
|
int
|
The later bracketing year. |
required |
target_year
|
int
|
The year actually requested ( |
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 |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per geography id present in both inputs, with each of |
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |