Skip to content

Commit

Permalink
Use context manager for reading Excel file
Browse files Browse the repository at this point in the history
  • Loading branch information
phackstock committed Feb 27, 2024
1 parent 0762055 commit ce40882
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions pyam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,36 +98,36 @@ 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:
sheets = kwargs.pop("sheet_name", sheet_name)
# 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])

# read single sheet (if only one exists in file) ignoring sheet name
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):
Expand Down

0 comments on commit ce40882

Please sign in to comment.