pytidycensus.variables
Census variable loading and caching functionality.
Functions
|
Clear the variables cache. |
|
Get all variables for a specific table. |
|
List available datasets for a given year. |
|
Load Census variables for a given dataset and year. |
|
Search for variables by pattern in labels, concepts, or names. |
- pytidycensus.variables.load_variables(year, dataset=None, survey=None, cache=True, cache_dir=None)[source]
Load Census variables for a given dataset and year.
- Parameters:
year (
int) – Census yeardataset (
Optional[str]) – Dataset name (‘acs’, ‘dec’, ‘pep’, etc.). Provide either dataset or survey.survey (
Optional[str]) – Survey type (e.g., ‘acs5’, ‘acs1’, ‘sf1’, ‘pl’). If provided, the dataset will be inferred from the survey. Provide either dataset or survey, not both.cache (
bool) – Whether to cache variables for faster future accesscache_dir (
Optional[str]) – Directory for caching. Defaults to user cache directory.
- Returns:
Variables with columns: name, label, concept, predicateType, group, limit
- Return type:
Examples
>>> # Load ACS 5-year variables for 2022 >>> acs_vars = load_variables(2022, "acs", "acs5") >>> >>> # Search for income-related variables >>> income_vars = acs_vars[acs_vars['label'].str.contains('income', case=False)] >>> >>> # Load decennial census variables for 2020 >>> dec_vars = load_variables(2020, "dec", "pl")
- pytidycensus.variables.search_variables(pattern, year, dataset, survey=None, field='concept')[source]
Search for variables by pattern in labels, concepts, or names.
- Parameters:
pattern (
str) – Search pattern (case-insensitive)year (
int) – Census yeardataset (
str) – Dataset name (‘acs’, ‘dec’, ‘pep’, etc.). Provide either dataset or survey.survey (
Optional[str]) – Survey type (e.g., ‘acs5’, ‘acs1’, ‘sf1’, ‘pl’). If provided, the dataset will be inferred from the survey. Provide either dataset or survey, not both.field (
str) – Field to search in (‘label’, ‘concept’, ‘name’, or ‘all’)
- Returns:
Matching variables
- Return type:
Examples
>>> # Search for income variables in ACS >>> income_vars = search_variables("income", 2022, "acs", "acs5") >>> >>> # Search for population in concepts >>> pop_vars = search_variables("population", 2020, "dec", "pl", field="concept")
- pytidycensus.variables.get_table_variables(table, year, dataset, survey=None)[source]
Get all variables for a specific table.
- Parameters:
- Returns:
Variables for the specified table
- Return type:
Examples
>>> # Get all variables for median household income table >>> b19013_vars = get_table_variables("B19013", 2022, "acs", "acs5") >>> >>> # Get all variables for race table in 2020 Census >>> p1_vars = get_table_variables("P1", 2020, "dec", "pl")