Skip to content

Commit

Permalink
Add --sheet-name to load on econometrics and forecast (#4525)
Browse files Browse the repository at this point in the history
* Add sheet-name to econometrics load

* Also do forecasting load

* Handle error when sheet not found
  • Loading branch information
jmaslek authored Apr 3, 2023
1 parent 291344b commit f9e6b20
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
11 changes: 10 additions & 1 deletion openbb_terminal/common/common_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
@log_start_end(log=logger)
def load(
file: str,
sheet_name: Optional[str] = None,
data_files: Optional[Dict[Any, Any]] = None,
data_examples: Optional[Dict[Any, Any]] = None,
) -> pd.DataFrame:
Expand Down Expand Up @@ -90,7 +91,15 @@ def load(

try:
if file_type == ".xlsx":
data = pd.read_excel(full_file)
try:
data = (
pd.read_excel(full_file)
if sheet_name is None
else pd.read_excel(full_file, sheet_name=sheet_name)
)
except ValueError as err:
console.print(f"[red]{err}[/red]\n")
return pd.DataFrame()
elif file_type == ".csv":
data = pd.read_csv(full_file)
else:
Expand Down
17 changes: 16 additions & 1 deletion openbb_terminal/econometrics/econometrics_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def __init__(self, queue: Optional[List[str]] = None):
"-a": "-alias",
"--examples": None,
"-e": "--examples",
"--sheet-name": None,
}

for feature in ["export", "show", "desc", "clear", "index"]:
Expand Down Expand Up @@ -349,6 +350,13 @@ def call_load(self, other_args: List[str]):
default=False,
dest="examples",
)
parser.add_argument(
"--sheet-name",
dest="sheet_name",
default=None,
nargs="+",
help="Name of excel sheet to save data to. Only valid for .xlsx files.",
)

if other_args and "-" not in other_args[0][0]:
other_args.insert(0, "-f")
Expand Down Expand Up @@ -405,7 +413,14 @@ def call_load(self, other_args: List[str]):
)
return

data = common_model.load(file, self.DATA_FILES, common_model.DATA_EXAMPLES)
data = common_model.load(
file,
data_files=self.DATA_FILES,
data_examples=common_model.DATA_EXAMPLES,
sheet_name=" ".join(ns_parser.sheet_name)
if ns_parser.sheet_name
else None,
)

if not data.empty:
data.columns = data.columns.map(lambda x: x.lower().replace(" ", "_"))
Expand Down
17 changes: 16 additions & 1 deletion openbb_terminal/forecast/forecast_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,14 @@ def call_load(self, other_args: List[str]):
help="Alias name to give to the dataset",
type=str,
)
parser.add_argument(
"--sheet-name",
dest="sheet_name",
default=None,
nargs="+",
help="Name of excel sheet to save data to. Only valid for .xlsx files.",
)

# Load in any newly exported files
self.DATA_FILES = forecast_model.get_default_files()

Expand All @@ -796,7 +804,14 @@ def call_load(self, other_args: List[str]):
"[red]The file/dataset selected has already been loaded.[/red]\n"
)
return
data = common_model.load(file, self.DATA_FILES, {})
data = common_model.load(
file,
data_files=self.DATA_FILES,
data_examples={},
sheet_name=" ".join(ns_parser.sheet_name)
if ns_parser.sheet_name
else None,
)
if not data.empty:
self.files_full.append([ns_parser.file, ns_parser.alias])
self.load(alias, data)
Expand Down

0 comments on commit f9e6b20

Please sign in to comment.