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.2.tar.gz (12.2 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.2-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: inec-0.1.2.tar.gz
  • Upload date:
  • Size: 12.2 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.2.tar.gz
Algorithm Hash digest
SHA256 dfcbf3b7baf4cc4c89382e79c4ff9ded82cac04f7ae2c7ecc77712acbb6de91a
MD5 7b8a1b7b913b28b6780069b68531e77b
BLAKE2b-256 bc20af7a9883d7654db73bd9bbe8716ce783784b9a8db430e5b4c478680f52c1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: inec-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.1 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1d1a88075772feda04b0596327b5dea1daf8eb5058ce43dd3202c5aef28938c3
MD5 226a92c3ce226078cf69fe58e4783eb2
BLAKE2b-256 bc6a082b73c2d0541d83e6786d3150e84aa64bd6c25581f6018e7f69c59d7c6b

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