Fix Economy/Plot - Does not read stored datasets properly (#2112)

* fix plot databases

* update website doc

* display stored datasets in one line

* add indent

* allow sequential storage of data

* fix menu string

* prevent appending empty dfs

* black

* silence warning
This commit is contained in:
montezdesousa
2022-07-20 10:55:09 +01:00
committed by GitHub
parent d474bcc0f8
commit ca4541053f
4 changed files with 144 additions and 80 deletions

View File

@@ -16,6 +16,7 @@ from openbb_terminal import feature_flags as obbff
from openbb_terminal.decorators import log_start_end
from openbb_terminal.economy import (
alphavantage_view,
economy_helpers,
finviz_view,
nasdaq_model,
nasdaq_view,
@@ -159,6 +160,11 @@ class EconomyController(BaseController):
self.UNITS: Dict[Any, Dict[Any, Any]] = dict()
self.FRED_TITLES: Dict = dict()
self.DATASETS["macro"] = pd.DataFrame()
self.DATASETS["treasury"] = pd.DataFrame()
self.DATASETS["fred"] = pd.DataFrame()
self.DATASETS["index"] = pd.DataFrame()
if session and obbff.USE_PROMPT_TOOLKIT:
self.choices: dict = {c: {} for c in self.controller_choices}
self.choices["overview"] = {c: None for c in self.overview_options}
@@ -546,10 +552,7 @@ class EconomyController(BaseController):
if ns_parser.parameters and ns_parser.countries:
# Store data
(
self.DATASETS["macro"],
self.UNITS,
) = econdb_model.get_aggregated_macro_data(
(df, units,) = econdb_model.get_aggregated_macro_data(
parameters=ns_parser.parameters,
countries=ns_parser.countries,
start_date=ns_parser.start_date,
@@ -557,26 +560,37 @@ class EconomyController(BaseController):
convert_currency=ns_parser.convert_currency,
)
self.DATASETS["macro"].columns = [
"_".join(column) for column in self.DATASETS["macro"].columns
]
if not df.empty:
df.columns = ["_".join(column) for column in df.columns]
self.stored_datasets += (
f"\n macro : {self.DATASETS['macro'].columns[0]}"
)
self.DATASETS["macro"] = pd.concat([self.DATASETS["macro"], df])
# Display data just loaded
econdb_view.show_macro_data(
parameters=ns_parser.parameters,
countries=ns_parser.countries,
start_date=ns_parser.start_date,
end_date=ns_parser.end_date,
convert_currency=ns_parser.convert_currency,
raw=ns_parser.raw,
export=ns_parser.export,
)
# update units dict
for country, data in units.items():
if country not in self.UNITS:
self.UNITS[country] = {}
self.update_runtime_choices()
for key, value in data.items():
self.UNITS[country][key] = value
self.stored_datasets = (
economy_helpers.update_stored_datasets_string(self.DATASETS)
)
# Display data just loaded
econdb_view.show_macro_data(
parameters=ns_parser.parameters,
countries=ns_parser.countries,
start_date=ns_parser.start_date,
end_date=ns_parser.end_date,
convert_currency=ns_parser.convert_currency,
raw=ns_parser.raw,
export=ns_parser.export,
)
self.update_runtime_choices()
if obbff.ENABLE_EXIT_AUTO_HELP:
self.print_help()
@check_api_key(["API_FRED_KEY"])
def call_fred(self, other_args: List[str]):
@@ -659,24 +673,34 @@ class EconomyController(BaseController):
for series_id, data in series_dict.items():
self.FRED_TITLES[series_id] = f"{data['title']} ({data['units']})"
self.DATASETS["fred"] = fred_model.get_aggregated_series_data(
df = fred_model.get_aggregated_series_data(
series_dict, ns_parser.start_date, ns_parser.end_date
)
self.stored_datasets += (
f"\n fred : {self.DATASETS['fred'].columns[0]}"
)
if not df.empty:
self.DATASETS["fred"] = pd.concat(
[
self.DATASETS["fred"],
df,
]
)
fred_view.display_fred_series(
d_series=series_dict,
start_date=ns_parser.start_date,
end_date=ns_parser.end_date,
raw=ns_parser.raw,
export=ns_parser.export,
limit=ns_parser.limit,
)
self.stored_datasets = (
economy_helpers.update_stored_datasets_string(self.DATASETS)
)
self.update_runtime_choices()
fred_view.display_fred_series(
d_series=series_dict,
start_date=ns_parser.start_date,
end_date=ns_parser.end_date,
raw=ns_parser.raw,
export=ns_parser.export,
limit=ns_parser.limit,
)
self.update_runtime_choices()
if obbff.ENABLE_EXIT_AUTO_HELP:
self.print_help()
@log_start_end(log=logger)
def call_index(self, other_args: List[str]):
@@ -789,9 +813,8 @@ class EconomyController(BaseController):
return self.queue
if ns_parser.indices:
self.DATASETS["index"] = pd.DataFrame()
for index in ns_parser.indices:
self.DATASETS["index"][index] = yfinance_model.get_index(
for i, index in enumerate(ns_parser.indices):
df = yfinance_model.get_index(
index,
interval=ns_parser.interval,
start_date=ns_parser.start_date,
@@ -799,22 +822,29 @@ class EconomyController(BaseController):
column=ns_parser.column,
)
self.stored_datasets += (
f"\n index : {self.DATASETS['index'].columns[0]}"
)
if not df.empty:
self.DATASETS["index"][index] = df
yfinance_view.show_indices(
indices=ns_parser.indices,
interval=ns_parser.interval,
start_date=ns_parser.start_date,
end_date=ns_parser.end_date,
column=ns_parser.column,
raw=ns_parser.raw,
export=ns_parser.export,
returns=ns_parser.returns,
)
self.stored_datasets = (
economy_helpers.update_stored_datasets_string(self.DATASETS)
)
self.update_runtime_choices()
# display only once in the last iteration
if i == len(ns_parser.indices) - 1:
yfinance_view.show_indices(
indices=ns_parser.indices,
interval=ns_parser.interval,
start_date=ns_parser.start_date,
end_date=ns_parser.end_date,
column=ns_parser.column,
raw=ns_parser.raw,
export=ns_parser.export,
returns=ns_parser.returns,
)
self.update_runtime_choices()
if obbff.ENABLE_EXIT_AUTO_HELP:
self.print_help()
@log_start_end(log=logger)
def call_treasury(self, other_args: List[str]):
@@ -906,28 +936,40 @@ class EconomyController(BaseController):
.stack()
.to_frame()
)
self.DATASETS["treasury"] = pd.DataFrame(
df[0].values.tolist(), index=df.index
).T
self.DATASETS["treasury"].columns = [
"_".join(column) for column in self.DATASETS["treasury"].columns
]
self.stored_datasets += (
f"\n treasury : {self.DATASETS['treasury'].columns[0]}"
)
if not df.empty:
self.DATASETS["treasury"] = pd.concat(
[
self.DATASETS["treasury"],
pd.DataFrame(df[0].values.tolist(), index=df.index).T,
]
)
econdb_view.show_treasuries(
types=ns_parser.type,
maturities=ns_parser.maturity,
frequency=ns_parser.frequency,
start_date=ns_parser.start_date,
end_date=ns_parser.end_date,
raw=ns_parser.raw,
export=ns_parser.export,
)
cols = []
for column in self.DATASETS["treasury"].columns:
if isinstance(column, tuple):
cols.append("_".join(column))
else:
cols.append(column)
self.DATASETS["treasury"].columns = cols
self.update_runtime_choices()
self.stored_datasets = (
economy_helpers.update_stored_datasets_string(self.DATASETS)
)
econdb_view.show_treasuries(
types=ns_parser.type,
maturities=ns_parser.maturity,
frequency=ns_parser.frequency,
start_date=ns_parser.start_date,
end_date=ns_parser.end_date,
raw=ns_parser.raw,
export=ns_parser.export,
)
self.update_runtime_choices()
if obbff.ENABLE_EXIT_AUTO_HELP:
self.print_help()
@log_start_end(log=logger)
def call_ycrv(self, other_args: List[str]):
@@ -1068,11 +1110,11 @@ class EconomyController(BaseController):
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prog="plot",
description="This command can plot any data on two y-axes obtained from the macro, fred, index and "
"treasury commands. To be able to use this data, use the -st argument available within these "
"commands. For example 'macro -p GDP -c Germany Netherlands -st' will store the data for usage "
"in this command. Therefore, it allows you to plot different time series in one graph. You can use "
"the 'options' command to show the required arguments to be entered. The example above could be plotted "
"the following way: 'plot --y1 Germany_GDP --y2 Netherlands_GDP' or 'plot --y1 Germany_GDP Netherlands_GDP'",
"treasury commands. To be able to use this data, just load the available series from the previous "
"commands. For example 'macro -p GDP -c Germany Netherlands' will store the data for usage "
"in this command. Therefore, it allows you to plot different time series in one graph. "
"The example above could be plotted the following way: 'plot --y1 Germany_GDP --y2 Netherlands_GDP' "
"or 'plot --y1 Germany_GDP Netherlands_GDP'",
)
parser.add_argument(
"--y1",

View File

@@ -0,0 +1,24 @@
"""Economy helpers"""
__docformat__ = "numpy"
def update_stored_datasets_string(datasets) -> str:
stored_datasets_string = ""
for key in datasets:
# ensure that column name is not empty
if not datasets[key].columns.empty:
# update string
for i, col in enumerate(datasets[key].columns):
if i == 0:
stored_datasets_string += (
"\n"
+ " " * 2
+ key
+ " " * (len("Stored datasets") - len(key) - 2)
+ ": "
)
stored_datasets_string += col
else:
stored_datasets_string += ", " + col
return stored_datasets_string

View File

@@ -531,12 +531,12 @@ def get_index(
f"The chosen column is not available for {ticker}. Please choose "
f"between: {', '.join(index_data.columns)}\n"
)
return pd.Series()
return pd.Series(dtype="float64")
if index_data.empty or len(index_data) < 2:
console.print(
f"The chosen index {ticker}, returns no data. Please check if "
f"there is any data available.\n"
)
return pd.Series()
return pd.Series(dtype="float64")
return index_data[column]

View File

@@ -2,9 +2,7 @@
usage: plot [-y1 YAXIS1 [YAXIS1 ...]] [-y2 YAXIS2 [YAXIS2 ...]] [-h] [--export {csv,json,xlsx}] [--raw] [-l LIMIT]
```
This command can plot any data on two y-axes obtained from the macro, fred, index and treasury commands. To be able to use this data, use the -st argument available within these commands. For example 'macro -p GDP -c Germany
Netherlands -st' will store the data for usage in this command. Therefore, it allows you to plot different time series in one graph. You can use the 'options' command to show the required arguments to be entered. The example above
could be plotted the following way: 'plot -y1 Germany_GDP -y2 Netherlands_GDP' or 'plot -y1 Germany_GDP Netherlands_GDP'
This command can plot any data on two y-axes obtained from the macro, fred, index and treasury commands. To be able to use this data, just load the available series from the previous commands. For example 'macro -p GDP -c Germany Netherlands' will store the data for usage in this command. Therefore, it allows you to plot different time series in one graph. The example above could be plotted the following way: 'plot --y1 Germany_GDP --y2 Netherlands_GDP' or 'plot --y1 Germany_GDP Netherlands_GDP'
```
optional arguments: