Skip to main content

Method for accessing INEC data directly from Python

Project description

INEC Costa Rica - Python API Module

This module is a powerful Python wrapper designed to facilitate access, exploration, and downloading of microdata from the Instituto Nacional de Estadística y Censos (INEC) of Costa Rica. It allows researchers and data analysts to interact with the institutional catalog directly from their development environment.


1. Structure & Hierarchy

The module is organized into three main levels to navigate the INEC database:

Level Manager Class Description
Repositories RepositoryManager High-level collections (e.g., REGENAHO, ENIG, Censos).
Datasets DatasetsManager Specific studies or surveys defined by a year or round.
Variables VariablesManager Metadata and data dictionaries for columns within a dataset.

2. Initialization

To use this module, you must have an active account on the INEC portal.

Obtaining your API Key

  1. Go to the INEC Profile Page.
  2. Log in with your credentials.
  3. Look for the API Key section to generate or copy your unique token.

Initialization

The autofill_msg parameter is used to automatically complete the "Purpose of Request" forms required by many datasets.

from inec import INECAPI

client = INECAPI(
    api_key="YOUR_API_KEY", 
    email="your@email.com", 
    password="your_password", 
    autofill_msg='I want to analyze these variables for time-series research'
)

3. Usage Examples

Repositories (client.repositories)

# List all available repositories
repo = client.repositories.show()
print(repo)

# Automatically fetch and concatenate multiple years of DBs
# (Searches the 'REGENAHO' repository between 2023 and 2025)
dataset = client.repositories.get(repositoryid='REGENAHO', start_year=2023, end_year=2025)
print(dataset)
# "DATA INTEGRITY NOTICE: Performing blind concatenation of multiple datasets. "
# "This module does not validate schema consistency, column names, or data types "
# "across different years. Please verify that variables are harmonized before analysis."
# "'_source_idno' column is created by the module for track which file produce it"

Datasets (client.datasets)

# List all datasets
all_datasets = client.datasets.show()
print(all_datasets)

# Search for datasets within a repository
dataset = client.datasets.show(repositoryid='REGENAHO')
print(dataset)

# Download microdata using internal ID or IDNO string (you can find this ids in their website or using datasets.show() function)
df_1 = client.datasets.get(113)
df_2 = client.datasets.get("INEC-Enaho-2025")

# List resources (additional files, questionnaires, etc.) for a study
resources = client.datasets.resources(113)
print(resources)

Variables (client.variables)

# View summary of variables for a study (very general view...)
var_summary = client.variables.show(128)
print(var_summary)

# Get full dictionary (ID, description, and documentation links) using internal ID or IDNO string
variables_1 = client.variables.dictionary("INEC-Enaho-2025")
print(variables_1)

variables_2 = client.variables.dictionary(113)
print(variables_2)

4. Key Features

  • Form Autofill: If a dataset requires a justification form, the module submits it automatically using your autofill_msg.

  • Automatic Parsing: Detects .sav (SPSS), .dta (Stata), .csv, and .xlsx extensions and converts them instantly to Pandas DataFrames. (some xlsx could have multiple sheets, be aware of that)

  • Session Management: Automatically handles cookie expiration and retries authentication if Unauthorized error occurs.

  • Data Integrity: When using repositories.get(), a _source_idno column is added to help track the origin of each row in concatenated datasets.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

inec-0.1.1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

inec-0.1.1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file inec-0.1.1.tar.gz.

File metadata

  • Download URL: inec-0.1.1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for inec-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4c34ddad037894451a78de1df010248d17be3c7cafe4aebeb821237b82eca54a
MD5 a2dd85a7266e61584c400499009bc285
BLAKE2b-256 90dde09712a800e9754a8ca52a99b7a8b75904586f82c6f8772a79ea43955478

See more details on using hashes here.

File details

Details for the file inec-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: inec-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for inec-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aa034b45fdfa087ff1eb20b84993a3cd72f22e979b1161f375e3672e1bb930a5
MD5 5794d69d55de4cc70c19da750994c1cc
BLAKE2b-256 824261d864e47d8c3f94d5234da0b998d65c2cc7f341e691ad6501e8677dee76

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page