Skip to content

Commit

Permalink
Economy menu bugs <> making the integration test work (#4563)
Browse files Browse the repository at this point in the history
* fix malformed dataframe on bigmac index for several countries

* improve handling of exception for gdp

* improve handling of exception for rgdp

* improve handling of exception for fgdp

* improve handling of exception for oecd model

* fix list index out of bonds

* add arg to fred
  • Loading branch information
hjoaquim authored Mar 23, 2023
1 parent 937d033 commit e69966b
Show file tree
Hide file tree
Showing 6 changed files with 486 additions and 412 deletions.
6 changes: 4 additions & 2 deletions openbb_terminal/common/quantitative_analysis/qa_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ def display_bw(
data = data.copy()
start = data[target].index[0]

color = theme.get_colors()[0]
colors = theme.get_colors()
color = colors[0]
pd.options.mode.chained_assignment = None
data["x_data"] = data[target].index.year if yearly else data[target].index.month
pd.options.mode.chained_assignment = "warn"
Expand Down Expand Up @@ -300,6 +301,7 @@ def display_bw(
for i, group in enumerate(data["x_data"].unique()):
x = group if yearly else l_months[group - 1]
y = data[data["x_data"] == group][target]

fig.add_box(
y=y,
x=[x] * len(y),
Expand All @@ -308,7 +310,7 @@ def display_bw(
color=theme.up_color,
outliercolor=theme.up_color,
),
fillcolor=theme.get_colors()[i],
fillcolor=colors[i % len(colors)],
line_color=color,
boxmean=True,
whiskerwidth=1,
Expand Down
1 change: 1 addition & 0 deletions openbb_terminal/economy/economy_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def __init__(self, queue: Optional[List[str]] = None):
self.DATASETS: Dict[Any, pd.DataFrame] = dict()
self.UNITS: Dict[Any, Dict[Any, Any]] = dict()
self.FRED_TITLES: Dict = dict()
self.choices: Dict = dict()

self.DATASETS["macro"] = pd.DataFrame()
self.DATASETS["treasury"] = pd.DataFrame()
Expand Down
15 changes: 9 additions & 6 deletions openbb_terminal/economy/nasdaq_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,21 @@ def get_big_mac_indices(country_codes: Optional[List[str]] = None) -> pd.DataFra
pd.DataFrame
Dataframe with Big Mac indices converted to USD equivalent.
"""
big_mac = pd.DataFrame()

if country_codes is None:
country_codes = ["USA"]

df_cols = ["Date"]
df_cols.extend(country_codes)
big_mac = pd.DataFrame(columns=df_cols)
dfs = []
for country in country_codes:
df1 = get_big_mac_index(country)
if not df1.empty:
big_mac[country] = df1["dollar_price"]
big_mac["Date"] = df1["Date"]
big_mac.set_index("Date", inplace=True)
df1 = df1.rename(columns={"dollar_price": country})
df1 = df1.set_index("Date")
dfs.append(df1)
if dfs:
big_mac = pd.concat(dfs, axis=1)
big_mac = big_mac.reset_index()
big_mac = big_mac.set_index("Date")

return big_mac
161 changes: 108 additions & 53 deletions openbb_terminal/economy/oecd_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,11 @@
}


def no_data_message(error: str):
"""Print message when no data available or error"""
console.print(f"Error getting data from OECD: [red]{error}[/red]")


@log_start_end(log=logger)
def get_gdp(
countries: Optional[List[str]],
Expand Down Expand Up @@ -519,11 +524,17 @@ def get_gdp(
"or MLN_USD (millions of US dollars) for units"
)

df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GDP.TOT.{units}.A/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
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"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
except Exception as e:
no_data_message(error=str(e))
return df

df = df.iloc[:, [0, 5]]

Expand Down Expand Up @@ -601,12 +612,18 @@ def get_real_gdp(
"for units"
)

df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.QGDP."
f"{'VOLIDX' if units == 'IDX' else 'TOT'}.{units}.Q/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
df = pd.DataFrame()

try:
df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.QGDP."
f"{'VOLIDX' if units == 'IDX' else 'TOT'}.{units}.Q/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
except Exception as e:
no_data_message(error=str(e))
return df

df = df.iloc[:, [0, 5]]

Expand Down Expand Up @@ -681,18 +698,24 @@ def get_gdp_forecast(
if types not in ["real", "nominal"]:
return console.print("Use either 'real' or 'nominal' for type")

if types == "real":
df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.REALGDPFORECAST.TOT.AGRWTH.{units}/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
else:
df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.NOMGDPFORECAST.TOT.AGRWTH.{units}/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
df = pd.DataFrame()

try:
if types == "real":
df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.REALGDPFORECAST.TOT.AGRWTH.{units}/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
else:
df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.NOMGDPFORECAST.TOT.AGRWTH.{units}/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
except Exception as e:
no_data_message(error=str(e))
return df

df = df.iloc[:, [0, 5]]

Expand Down Expand Up @@ -762,11 +785,17 @@ def get_debt(
elif isinstance(end_date, datetime):
end_date = end_date.date()

df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GGDEBT.TOT.PC_GDP.A/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
df = pd.DataFrame()

try:
df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GGDEBT.TOT.PC_GDP.A/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
except Exception as e:
no_data_message(error=str(e))
return df

df = df.iloc[:, [0, 5]]

Expand Down Expand Up @@ -856,11 +885,16 @@ def get_cpi(
if units not in ["AGRWTH", "IDX2015"]:
return console.print("Use either 'AGRWTH' or 'IDX2015' for type")

df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.CPI.{perspective}.{units}.{frequency}/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
df = pd.DataFrame()
try:
df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.CPI.{perspective}.{units}.{frequency}/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
except Exception as e:
no_data_message(error=str(e))
return df

df = df.iloc[:, [0, 5]]

Expand Down Expand Up @@ -931,11 +965,16 @@ def get_balance(
elif isinstance(end_date, datetime):
end_date = end_date.date()

df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GGNLEND.TOT.PC_GDP.A/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
df = pd.DataFrame()
try:
df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GGNLEND.TOT.PC_GDP.A/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
except Exception as e:
no_data_message(error=str(e))
return df

df = df.iloc[:, [0, 5]]

Expand Down Expand Up @@ -1008,11 +1047,16 @@ def get_revenue(
"or PC_GDP (percentage of GDP) for units"
)

df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GGREV.TOT.{units}.A/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
df = pd.DataFrame()
try:
df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GGREV.TOT.{units}.A/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
except Exception as e:
no_data_message(error=str(e))
return df

df = df.iloc[:, [0, 5]]

Expand Down Expand Up @@ -1117,11 +1161,17 @@ def get_spending(
"EDU (Education), ENVPROT (Environmental protection), GRALPUBSER (General public services), "
"SOCPROT (Social protection), ECOAFF (Economic affairs), DEF (Defence), HEALTH (Health)"
)
df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GGEXP.{perspective}.{units}.A/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)

df = pd.DataFrame()
try:
df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.GGEXP.{perspective}.{units}.A/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
except Exception as e:
no_data_message(error=str(e))
return df

df = df.iloc[:, [0, 5]]

Expand Down Expand Up @@ -1184,11 +1234,16 @@ def get_trust(
elif isinstance(end_date, datetime):
end_date = end_date.date()

df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.TRUSTGOV.TOT.PC.A/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
df = pd.DataFrame()
try:
df = pd.read_csv(
f"https://stats.oecd.org/sdmx-json/data/DP_LIVE/.TRUSTGOV.TOT.PC.A/OECD?contentType=csv&detail=code"
f"&separator=comma&csv-lang=en&startPeriod={start_date}&endPeriod={end_date}",
index_col=5,
)
except Exception as e:
no_data_message(error=str(e))
return df

df = df.iloc[:, [0, 5]]

Expand Down
Loading

0 comments on commit e69966b

Please sign in to comment.