hotfix/gdp-units - better GDP units (#5262)

* better GDP units

* mypy

* remove not needed dict

* test_oecd_view.py

* test_oecd_model.py
This commit is contained in:
Danglewood
2023-07-31 09:05:22 -07:00
committed by GitHub
parent dc30ab3d36
commit d212a8e270
8 changed files with 42 additions and 31 deletions

View File

@@ -596,9 +596,9 @@ class EconomyController(BaseController):
"--units",
type=str,
dest="units",
help="Use either USD_CAP (US dollars per capita) or MLN_USD (millions of US dollars)",
choices=["USD_CAP", "MLN_USD"],
default="USD_CAP",
help="Use either USD or USD_CAP (USD Per Capita)",
choices=["USD", "USD_CAP"],
default="USD",
)
parser.add_argument(

View File

@@ -473,8 +473,8 @@ def no_data_message(error: str):
@log_start_end(log=logger)
def get_gdp(
countries: Optional[List[str]],
units: str = "USD_CAP",
countries: Optional[str] = "united_states",
units: str = "USD",
start_date="",
end_date="",
) -> pd.DataFrame:
@@ -496,7 +496,7 @@ def get_gdp(
countries: list
List of countries to get data for
units: str
Units to get data in. Either 'USD_CAP' or 'MLN_USD'.
Units to get data in. Either 'USD' or 'USD_CAP'.
Default is US dollars per capita.
start_date: str
Start date of data, in YYYY-MM-DD format
@@ -518,17 +518,21 @@ def get_gdp(
if isinstance(end_date, datetime):
end_date = end_date.date()
if units not in ["USD_CAP", "MLN_USD"]:
return console.print(
"Use either USD_CAP (US dollars per capita) "
"or MLN_USD (millions of US dollars) for units"
units_dict = {"USD": "MLN_USD", "USD_CAP": "USD_CAP"}
if units not in units_dict:
console.print(
"Invalid choice, choices are either USD or USD_CAP. Defaulting to USD."
)
units = "USD"
countries = [countries] if isinstance(countries, str) else countries # type: ignore[assignment]
df = pd.DataFrame()
try:
df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GDP.TOT.{units}.A/OECD?contentType=csv&detail=code"
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GDP.TOT.{units_dict[units]}.A/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
@@ -552,6 +556,7 @@ def get_gdp(
result.index = pd.to_datetime(result.index, format="%Y")
result.sort_index(inplace=True)
result = result * 1000000 if units == "USD" else result
return result

View File

@@ -21,19 +21,19 @@ YAXIS_TITLES = {
"AGRWTH": "Annual Growth Rate (%)",
"IDX": "Index (Base = 2015)",
"IDX2015": "Index (Base = 2015)",
"MLN_USD": "Millions of US Dollars",
"USD": "Nominal Value in USD",
"PC_CHGPY": "Same Quarter of the Previous Year (% Change)",
"PC_CHGPP": "Previous Quarter (% Change)",
"PC_GDP": "Percentage of GDP (%)",
"THND_USD_CAP": "Thousands of USD per Capita",
"USD_CAP": "US Dollars per Capita",
"USD_CAP": "USD per Capita",
}
@log_start_end(log=logger)
def plot_gdp(
countries: Optional[List[str]],
units: str = "USD_CAP",
countries: Optional[str] = "united_states",
units: str = "USD",
start_date: str = "",
end_date: str = "",
raw: bool = False,
@@ -59,7 +59,7 @@ def plot_gdp(
countries: list
List of countries to get data for
units: str
Units to get data in. Either 'USD_CAP' or 'MLN_USD'.
Units to get data in. Either 'USD' or 'USD_CAP'.
Default is US dollars per capita.
start_date: str
Start date of data, in YYYY-MM-DD format
@@ -79,11 +79,13 @@ def plot_gdp(
Union[OpenBBFigure, None]
OpenBBFigure object if external_axes is True, else None (opens plot in a window)
"""
if units not in ["USD_CAP", "MLN_USD"]:
return console.print(
"Use either USD_CAP (US dollars per capita) "
"or MLN_USD (millions of US dollars) for units"
countries = [countries] if isinstance(countries, str) else countries # type: ignore[assignment]
if units not in ["USD", "USD_CAP"]:
console.print(
"Invalid choice, choices are either USD or USD_CAP. Defaulting to USD."
)
units = "USD"
df = oecd_model.get_gdp(countries, units, start_date, end_date)
@@ -102,7 +104,8 @@ def plot_gdp(
showlegend=True,
)
title = "Nominal Gross Domestic Product (GDP)"
title = "Nominal Gross Domestic Product (USD)"
title = title + " Per Capita" if units == "USD_CAP" else title
fig.set_title(title)
if raw:

File diff suppressed because one or more lines are too long

View File

@@ -1,3 +1,3 @@
TIME,united_states,canada
2020-01-01,63480.861222,47265.698235
2021-01-01,70181.12175,53101.315002
2020-01-01,21060474000000.0,1794940001000.0
2021-01-01,23315081000000.0,2026899115000.0
1 TIME united_states canada
2 2020-01-01 63480.861222 21060474000000.0 47265.698235 1794940001000.0
3 2021-01-01 70181.12175 23315081000000.0 53101.315002 2026899115000.0

View File

@@ -609,7 +609,7 @@ def test_call_func_expect_queue(expected_queue, func, queue):
countries=["united states"],
start_date=datetime.datetime(2021, 1, 1, 0, 0),
end_date=datetime.datetime(2023, 1, 1, 0, 0),
units="USD_CAP",
units="USD",
raw=False,
export="csv",
sheet_name=None,

View File

@@ -11,8 +11,8 @@ def test_get_gdp(recorder):
"""Test get_gdp"""
data = oecd_model.get_gdp(
countries=["united_states", "canada"],
start_date=datetime.strptime("2020-01-01", "%Y-%m-%d"),
end_date=datetime.strptime("2022-01-31", "%Y-%m-%d"),
start_date="2020-01-01",
end_date="2022-01-31",
)
assert isinstance(data, pd.DataFrame)
recorder.capture(data)

View File

@@ -9,7 +9,10 @@ from openbb_terminal.economy import oecd_view
@pytest.mark.parametrize(
"countries, kwargs",
[
(["united_states"], {"start_date": "2019-01-01", "end_date": "2021-12-31"}),
(
["united_states"],
{"units": "USD_CAP", "start_date": "2019-01-01", "end_date": "2021-12-31"},
),
],
)
def test_plot_gdp(countries, kwargs):