Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --sheet-name to load on econometrics and forecast #4525

Merged
merged 14 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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