-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add e-Stat provider for Japan government statistical data #7215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
YenAle-FT-Gmail
wants to merge
15
commits into
OpenBB-finance:develop
Choose a base branch
from
YenAle-FT-Gmail:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e35d652
feat: Add e-Stat provider for Japanese government statistical data
YenAle-FT-Gmail 6bf0102
config setup for local testing
YenAle-FT-Gmail 492f4e0
feat: Add e-Stat provider for Japanese government statistical data
YenAle-FT-Gmail 9190952
Delete test_estat_integration.py
YenAle-FT-Gmail 897fa9c
Merge branch 'develop' into develop
deeleeramone 82ce642
Merge branch 'develop' into develop
deeleeramone e18de90
fix: Correct e-Stat provider credential handling
YenAle-FT-Gmail 407c123
docs: Improve README and remove broken data_format parameter
YenAle-FT-Gmail 4f8c79b
Merge branch 'OpenBB-finance:develop' into develop
YenAle-FT-Gmail 05b0daf
Merge branch 'develop' into develop
deeleeramone 50ae29e
implement search
YenAle-FT-Gmail 3836bfc
Merge branch 'develop' into develop
deeleeramone 9776727
Merge branch 'develop' into develop
deeleeramone 1fa9b28
Merge branch 'develop' into develop
deeleeramone a33bc8d
Merge branch 'develop' into develop
deeleeramone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
openbb_platform/core/openbb_core/provider/standard_models/estat_search.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| """e-Stat Search Model.""" | ||
|
|
||
| from datetime import date as dateType | ||
| from typing import Optional | ||
|
|
||
| from openbb_core.provider.abstract.data import Data | ||
| from openbb_core.provider.abstract.query_params import QueryParams | ||
| from pydantic import Field | ||
|
|
||
|
|
||
| class SearchQueryParams(QueryParams): | ||
| """e-Stat Search Query Params.""" | ||
|
|
||
| query: Optional[str] = Field( | ||
| default=None, | ||
| description="Search keyword(s) for datasets. If not provided, returns all available datasets.", | ||
| ) | ||
|
|
||
|
|
||
| class SearchData(Data): | ||
| """e-Stat Search Data.""" | ||
|
|
||
| dataset_id: str = Field( | ||
| description="Statistical dataset ID (e.g., '0003433219'). Use this with estat_series() to fetch data." | ||
| ) | ||
| title: Optional[str] = Field( | ||
| default=None, | ||
| description="Dataset title in English (when available) or Japanese.", | ||
| ) | ||
| stats_code: Optional[str] = Field( | ||
| default=None, | ||
| description="Statistical survey code (e.g., '00200521').", | ||
| ) | ||
| gov_org: Optional[str] = Field( | ||
| default=None, | ||
| description="Government organization responsible for the dataset.", | ||
| ) | ||
| statistics_name: Optional[str] = Field( | ||
| default=None, | ||
| description="Name of the statistical survey or collection.", | ||
| ) | ||
| survey_date: Optional[dateType] = Field( | ||
| default=None, | ||
| description="Survey or data collection date.", | ||
| ) | ||
| open_date: Optional[dateType] = Field( | ||
| default=None, | ||
| description="Date the dataset was made publicly available.", | ||
| ) | ||
| small_area: Optional[str] = Field( | ||
| default=None, | ||
| description="Whether dataset includes small area statistics (0=No, 1=Yes).", | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| # OpenBB e-Stat Provider | ||
|
|
||
| ## Overview | ||
|
|
||
| The e-Stat provider integrates Japan's official government statistical portal API into OpenBB. [e-Stat](https://www.e-stat.go.jp/en) is operated by the Ministry of Internal Affairs and Communications and provides access to 28+ statistical databases including population census, labor surveys, economic indicators, trade statistics, and regional data. | ||
|
|
||
| **Attribution Notice**: This service uses API functions from e-Stat, however its contents are not guaranteed by government. | ||
|
|
||
| ## Setup Instructions | ||
|
|
||
| ### Step 1: Register for e-Stat Account | ||
|
|
||
| 1. Visit: https://www.e-stat.go.jp/en/mypage/user/preregister | ||
| 2. Complete registration with your email | ||
| 3. Confirm your email and log in | ||
|
|
||
| ### Step 2: Get Your Application ID (API Key) | ||
|
|
||
| 1. After logging in, go to "My Page" (top right corner) | ||
| 2. Click on "API functions" section | ||
| 3. Fill in the simple form: | ||
| - **Name**: Your app name | ||
| - **URL**: Your URL (use `http://test.localhost/` if not public) | ||
| - **Overview**: Brief description (optional) | ||
| 4. Click "Issue" button | ||
| 5. Your Application ID will be displayed immediately | ||
|
|
||
|  | ||
| *Example of the API functions dashboard showing Application IDs* | ||
|
|
||
| ### Step 3: Configure Your API Key | ||
|
|
||
| ```python | ||
| from openbb import obb | ||
|
|
||
| # Set your API key (required) | ||
| obb.account.credentials.estat_api_key = "your_application_id_here" | ||
| ``` | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ```python | ||
| from openbb import obb | ||
|
|
||
| # Search for datasets | ||
| results = obb.economy.estat_search(query="population", provider="estat") | ||
| dataset_id = results.results[0].dataset_id | ||
|
|
||
| # Get Japanese population census data using the dataset ID | ||
| data = obb.economy.estat_series( | ||
| provider="estat", | ||
| symbol=dataset_id, | ||
| ) | ||
|
|
||
| # Or use a known dataset ID directly | ||
| data = obb.economy.estat_series( | ||
| provider="estat", | ||
| symbol="0003433219", # Population Census dataset ID | ||
| area_code="13000", # Tokyo Prefecture | ||
| start_date="2020-01", | ||
| end_date="2020-12" | ||
| ) | ||
| ``` | ||
|
|
||
| ## Finding Your Data | ||
|
|
||
| ### Understanding the Data Structure | ||
|
|
||
| Think of e-Stat as Japan's official government data library. To use it effectively, understand these key concepts: | ||
|
|
||
| **Dataset IDs** (like `0003433219`): Unique identifiers for each data collection. You'll find these on the e-Stat website when browsing datasets. | ||
|
|
||
| **Area Codes**: Geographic filters | ||
| - `00000` = All of Japan (national data) | ||
| - `13000` = Tokyo Prefecture | ||
| - `27000` = Osaka Prefecture | ||
| - City codes are longer (e.g., `13101` for Chiyoda-ku, Tokyo) | ||
|
|
||
| **Stats Codes**: Survey identifiers (e.g., `00200521` for Population Census) | ||
|
|
||
| **Category Codes**: Vary by dataset - check the specific dataset documentation for valid values | ||
|
|
||
| ### Three Ways to Find Dataset IDs | ||
|
|
||
| #### Method 1: Browse the Website (Easiest) | ||
|
|
||
| 1. Go to: https://www.e-stat.go.jp/en/stat-search/database | ||
| 2. Use categories like "Population and Households", "Labor and Wages", "Prices" | ||
| 3. Click on a dataset to view details | ||
| 4. The dataset ID appears in the URL and dataset information | ||
|
|
||
| #### Method 2: Search by Keywords | ||
|
|
||
| Use the search box on the e-Stat website with English terms like "population", "unemployment", "consumer price" | ||
|
|
||
| #### Method 3: Programmatic Search (Recommended) | ||
|
|
||
| Use the built-in search function to discover datasets directly within OpenBB: | ||
|
|
||
| ```python | ||
| from openbb import obb | ||
|
|
||
| # Search for datasets by keyword | ||
| results = obb.economy.estat_search(query="population census", provider="estat") | ||
|
|
||
| # View search results | ||
| for dataset in results.results: | ||
| print(f"ID: {dataset.dataset_id}") | ||
| print(f"Title: {dataset.title}") | ||
| print(f"Stats Code: {dataset.stats_code}") | ||
| print(f"Organization: {dataset.gov_org}") | ||
| print("---") | ||
|
|
||
| # Access metadata and attribution | ||
| print(results.extra['results_metadata']['attribution']) | ||
|
|
||
| # Use a dataset ID from search results to fetch data | ||
| dataset_id = results.results[0].dataset_id | ||
| data = obb.economy.estat_series(symbol=dataset_id, provider="estat") | ||
| ``` | ||
|
|
||
| **Search Options:** | ||
| - Omit `query` to get all available datasets | ||
| - Use `limit` parameter to control number of results (default: 100, e-Stat API default: 10,000) | ||
| - Use `start_position` for pagination (starts at 1). Get next page position from `response.extra['results_metadata']['next_start_position']` | ||
| - Use `updated_date` to filter by dataset update date (format: YYYY-MM-DD) | ||
|
|
||
| **Pagination Example:** | ||
| ```python | ||
| # Get first 100 results | ||
| page1 = obb.economy.estat_search(query="population", limit=100, provider="estat") | ||
|
|
||
| # Check if more results available | ||
| if page1.extra['results_metadata'].get('has_more'): | ||
| next_position = page1.extra['results_metadata']['next_start_position'] | ||
| # Get next 100 results | ||
| page2 = obb.economy.estat_search( | ||
| query="population", | ||
| limit=100, | ||
| start_position=next_position, | ||
| provider="estat" | ||
| ) | ||
| ``` | ||
|
|
||
| ### Common Datasets | ||
|
|
||
| **Population Census**: `0003433219` and similar IDs | ||
| - Complete population count every 5 years | ||
| - Includes demographics by prefecture/city | ||
|
|
||
| **Consumer Price Index**: Various IDs in the `0003` series | ||
| - Monthly inflation data | ||
| - Price changes by category | ||
|
|
||
| **Labor Force Survey**: Multiple IDs in the `0003` series | ||
| - Monthly employment statistics | ||
| - Unemployment rates | ||
|
|
||
| Note: Dataset IDs can change when surveys are updated. Always verify current IDs on the e-Stat website. | ||
|
|
||
| ## Parameters | ||
|
|
||
| - `symbol`: Statistical dataset ID (required) - the unique identifier from e-Stat | ||
| - `stats_code`: Survey code like `00200521` for specific surveys | ||
| - `area_code`: Geographic filter (see area codes above) | ||
| - `category_code`: Dataset-specific category filters | ||
| - `search_kind`: "1" for standard statistics, "2" for regional mesh statistics | ||
| - `collect_area`: Collection area for aggregated data | ||
| - `start_date` / `end_date`: Date range in YYYY-MM format | ||
|
|
||
| **Note**: This provider currently only supports JSON format responses. | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| **"Invalid Application ID" Error** | ||
| - Verify your API key from the e-Stat API functions page | ||
| - Ensure you've set `obb.account.credentials.estat_api_key` | ||
|
|
||
| **"Statistical data not found" Error** | ||
| - Check the dataset ID is current (IDs can change) | ||
| - Not all parameters work with all datasets | ||
| - Try removing optional parameters | ||
|
|
||
| **Japanese Text in Responses** | ||
| - Normal for some metadata despite `lang=E` parameter | ||
| - Numerical data is universal | ||
|
|
||
| **Empty Data** | ||
| - Some datasets update infrequently (annually or every 5 years) | ||
| - Check the e-Stat website for update schedules | ||
|
|
||
| ## API Reference | ||
|
|
||
| - **API Guide**: https://www.e-stat.go.jp/api/api/index.php/en/api-info/api-guide | ||
| - **API Specification**: https://www.e-stat.go.jp/api/api/index.php/en/api-info/api-spec | ||
| - **Database Search**: https://www.e-stat.go.jp/en/stat-search/database | ||
|
|
||
| ## Development | ||
|
|
||
| ### Running Tests | ||
|
|
||
| ```bash | ||
| cd openbb_platform/providers/estat | ||
| poetry install | ||
| poetry run pytest tests/ | ||
| ``` | ||
|
|
||
| ### Contributing | ||
|
|
||
| Contributions are welcome! Areas for improvement: | ||
| - Additional data fetchers for specific statistical categories | ||
| - Enhanced date parsing for Japanese fiscal periods | ||
| - Caching for frequently accessed metadata | ||
|
|
||
| Please ensure all contributions maintain the attribution notice and comply with e-Stat's terms of use. | ||
|
|
||
| ## License & Attribution | ||
|
|
||
| When using this provider, you must acknowledge: | ||
|
|
||
| > This service uses API functions from e-Stat, however its contents are not guaranteed by government. | ||
|
|
||
| This attribution is required by e-Stat's terms of use: https://www.e-stat.go.jp/api/api/index.php/en/api-info/credit | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.