From ce40882df2d0838b9d40b27ff6cf7ab7e4741a9a Mon Sep 17 00:00:00 2001 From: Philip Hackstock <20710924+phackstock@users.noreply.github.com> Date: Tue, 27 Feb 2024 16:24:21 +0100 Subject: [PATCH] Use context manager for reading Excel file --- pyam/utils.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pyam/utils.py b/pyam/utils.py index 6bfefe616..015b24535 100644 --- a/pyam/utils.py +++ b/pyam/utils.py @@ -98,9 +98,7 @@ def read_pandas(path, sheet_name=["data*", "Data*"], *args, **kwargs): if isinstance(path, Path) and path.suffix == ".csv": return pd.read_csv(path, *args, **kwargs) - else: - xl = path if isinstance(path, pd.ExcelFile) else pd.ExcelFile(path) - + with pd.ExcelFile(path) as xl: # reading multiple sheets sheet_names = pd.Series(xl.sheet_names) if len(sheet_names) > 1: @@ -108,7 +106,9 @@ def read_pandas(path, sheet_name=["data*", "Data*"], *args, **kwargs): # apply pattern-matching for sheet names (use * as wildcard) sheets = sheet_names[pattern_match(sheet_names, values=sheets)] if sheets.empty: - raise ValueError(f"Sheet(s) '{sheet_name}' not found in file '{path}'.") + raise ValueError( + f"Sheet(s) '{sheet_name}' not found in file '{path}'." + ) df = pd.concat([xl.parse(s, *args, **kwargs) for s in sheets]) @@ -116,18 +116,18 @@ def read_pandas(path, sheet_name=["data*", "Data*"], *args, **kwargs): else: df = pd.read_excel(xl, *args, **kwargs) - # remove unnamed and empty columns, and rows were all values are nan - def is_empty(name, s): - if str(name).startswith("Unnamed: "): - try: - if len(s) == 0 or all(np.isnan(s)): - return True - except TypeError: - pass - return False + # remove unnamed and empty columns, and rows were all values are nan + def is_empty(name, s): + if str(name).startswith("Unnamed: "): + try: + if len(s) == 0 or all(np.isnan(s)): + return True + except TypeError: + pass + return False - empty_cols = [c for c in df.columns if is_empty(c, df[c])] - return df.drop(columns=empty_cols).dropna(axis=0, how="all") + empty_cols = [c for c in df.columns if is_empty(c, df[c])] + return df.drop(columns=empty_cols).dropna(axis=0, how="all") def read_file(path, *args, **kwargs):