Skip to content

Commit

Permalink
hotfix/gdp-units - better GDP units (#5262)
Browse files Browse the repository at this point in the history
* better GDP units

* mypy

* remove not needed dict

* test_oecd_view.py

* test_oecd_model.py
  • Loading branch information
deeleeramone authored Jul 31, 2023
1 parent dc30ab3 commit d212a8e
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 31 deletions.
6 changes: 3 additions & 3 deletions openbb_terminal/economy/economy_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,9 @@ def call_gdp(self, other_args: List[str]):
"--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(
Expand Down
21 changes: 13 additions & 8 deletions openbb_terminal/economy/oecd_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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,
)
Expand All @@ -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

Expand Down
23 changes: 13 additions & 10 deletions openbb_terminal/economy/oecd_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
"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,
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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:
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tests/openbb_terminal/economy/test_economy_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/openbb_terminal/economy/test_oecd_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion tests/openbb_terminal/economy/test_oecd_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
@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):
Expand Down

0 comments on commit d212a8e

Please sign in to comment.