Skip to content
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

Improving economy.cpi for the FRED data #5602

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import date as dateType
from typing import List, Literal, Optional

from dateutil import parser
from pydantic import Field, field_validator

from openbb_provider.abstract.data import Data
Expand Down Expand Up @@ -91,15 +92,10 @@ class CPIQueryParams(QueryParams):
class CPIData(Data):
"""CPI data."""

date: Optional[dateType] = Field(
default=None, description=DATA_DESCRIPTIONS.get("date")
)
value: Optional[float] = Field(default=None, description="CPI value on the date.")
date: dateType = Field(description=DATA_DESCRIPTIONS.get("date"))

@field_validator("value", mode="before")
@field_validator("date", mode="before")
@classmethod
def value_validate(cls, v: str): # pylint: disable=E0213
"""Validate value."""
if v == ".":
return 0.0
return float(v)
def date_validate(cls, v):
"""Validate date."""
return parser.isoparse(v)
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
"date": "A specific date to get data for.",
"limit": "The number of data entries to return.",
"countries": "The country or countries to get data.",
"units": "The data units.",
"frequency": "The data time frequency.",
"units": """The unit of measurement for the CPI data.
Options:
- `growth_previous`: growth from the previous period
- `growth_same`: growth from the same period in the previous year
- `index_2015`: index with base year 2015.""",
"frequency": "The frequency of the data points; options include `monthly`, `quarter`, and `annual`",
}

DATA_DESCRIPTIONS = {
Expand Down
38 changes: 22 additions & 16 deletions openbb_platform/providers/fred/openbb_fred/models/cpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@ class FREDCPIData(CPIData):
"""CPI data."""


class FREDCPIFetcher(Fetcher[FREDCPIQueryParams, List[Dict[str, List[FREDCPIData]]]]):
class FREDCPIFetcher(Fetcher[FREDCPIQueryParams, List[FREDCPIData]]):
"""FRED CPI Fetcher."""

data_type = FREDCPIData

@staticmethod
def transform_query(params: Dict[str, Any]) -> FREDCPIQueryParams:
return FREDCPIQueryParams(**params)

@staticmethod
def extract_data(
query: FREDCPIQueryParams, credentials: Optional[Dict[str, str]], **kwargs: Any
) -> dict:
) -> Dict:
api_key = credentials.get("fred_api_key") if credentials else ""

all_options = all_cpi_options(query.harmonized)
Expand All @@ -41,23 +39,31 @@ def extract_data(
series_dict = {}
fred = Fred(api_key)
for item in step_3:
loc = f"{item['country']}-{item['frequency']}-{item['units']}"
loc = f"{item['country']}"
temp = fred.get_series(
item["series_id"], query.start_date, query.end_date, **kwargs
)
series_dict[loc] = temp
temp = [{"date": item["date"], "value": item["value"]} for item in temp]
series_dict[loc] = [item for item in temp if item["value"] != "."]

return series_dict

@staticmethod
def transform_data(
query: FREDCPIQueryParams, data: dict, **kwargs: Any
) -> List[Dict[str, List[FREDCPIData]]]:
for key, value in data.items():
data[key] = [
FREDCPIData(date=x["date"], value=x["value"])
for x in value
if x["value"] != "."
]

return [data]
query: FREDCPIQueryParams, data: Dict, **kwargs: Any
) -> List[FREDCPIData]:
transformed_data = {}

# Iterate over the series_dict
for country, data_list in data.items():
for item in data_list:
# If the date is not in the dictionary, add it
if item["date"] not in transformed_data:
transformed_data[item["date"]] = {"date": item["date"]}
# Update the dictionary with the country's value data
transformed_data[item["date"]].update({country: item["value"]})

# Convert the dictionary to a list of dictionaries
transformed_data = list(transformed_data.values())

return [FREDCPIData.model_validate(item) for item in transformed_data]
Loading