pycensus.cache¶
Shared on-disk parquet cache: geometry and attribute columns, merged per level/region.
Technical aspects
One parquet file per (country, level, region) -- e.g.
cache_dir/usa/tract/25.parquet for Massachusetts tracts -- holds the
id column, geometry, and every attribute column ever fetched for it
across every source (dhc_*, acs_*, lodes_rac_*, ...).
:func:load_region checks which requested columns are already in that
file; only the columns actually missing are fetched (via a
caller-supplied callback) and merged back in before the file is
rewritten, so repeated loads -- even across different sources -- never
re-fetch a column already on disk.
This is deliberately independent of pygris' own cache: every pygris
call this package makes uses cache=False, so pygris never writes to
disk itself -- this module is pyCensus' only persistent cache.
Source
Parquet chosen for compact columnar storage and native GeoParquet
support via geopandas.read_parquet / GeoDataFrame.to_parquet.
get_cache_dir(namespace='', cache_dir=None, create=True)
¶
Resolve a cache folder without ever touching the current working directory.
Resolution order:
cache_dirif given -- returned exactly as passed (no namespace added).- Environment variable
PYCENSUS_CACHE_DIR-><env>/<namespace>. - A temporary folder:
<tempdir>/pycensus-cache/<namespace>.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str
|
Sub-folder name (may contain |
''
|
cache_dir
|
'Optional[Union[str, Path]]'
|
Explicit folder (highest priority). |
None
|
create
|
bool
|
Create the folder (and parents) if missing. |
True
|
get_data_dir(country, data_dir=None)
¶
Base folder for a country's raw downloads (<base>/raw/<source> is added by loaders).
Explicit data_dir is returned unchanged; otherwise
PYCENSUS_CACHE_DIR/data/<country> or <tempdir>/pycensus-cache/data/<country>.
The folder is not created here; loaders create sub-folders when they write.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country
|
str
|
Country module name ( |
required |
data_dir
|
'Optional[Union[str, Path]]'
|
Explicit folder; returned as is. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
The folder (not created). |
get_worldpop_dir(folder=None, create=True)
¶
Folder for WorldPop rasters: explicit folder, else $PYCENSUS_WORLDPOP_DIR, else the worldpop cache namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder
|
'Optional[Union[str, Path]]'
|
Explicit folder. |
None
|
create
|
bool
|
Create the folder if missing. |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
The folder. |
default_cache_dir()
¶
Default root of the parquet cache (env var or temp folder; see :func:get_cache_dir).
region_path(cache_dir, country, level, region)
¶
Build the cache file path for one (country, level, region) combination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Optional[str]
|
Root cache directory. Defaults to :func: |
required |
country
|
str
|
Country code, e.g. |
required |
level
|
str
|
Geometry level name. |
required |
region
|
str
|
Region key within that level (e.g. a state FIPS code). |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the merged parquet cache file for that combination. |
read_cache(path)
¶
Read a cached GeoDataFrame, or None if the file doesn't exist yet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Parquet file. |
required |
Returns:
| Type | Description |
|---|---|
Optional[GeoDataFrame]
|
The GeoDataFrame, or None. |
write_cache(path, gdf)
¶
Write a GeoDataFrame to the cache, creating parent directories as needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Parquet file. |
required |
gdf
|
GeoDataFrame
|
Data to write. |
required |
load_region(path, id_col, columns, fetch_boundary, fetch_columns, use_cache=True)
¶
Load one level/region, fetching and persisting only what's missing from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Cache file path, from :func: |
required |
id_col
|
str
|
Id column shared by boundary and attribute data (e.g. |
required |
columns
|
List[str]
|
Attribute column names the caller needs. |
required |
fetch_boundary
|
Callable[[], GeoDataFrame]
|
Called to fetch geometry -- only when |
required |
fetch_columns
|
Callable[[List[str]], 'pd.DataFrame']
|
Called with the subset of |
required |
use_cache
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
GeoDataFrame with |
GeoDataFrame
|
|