From 3d13de440175d9287015814f461bc8041c5118fa Mon Sep 17 00:00:00 2001 From: Philip Hackstock <20710924+phackstock@users.noreply.github.com> Date: Wed, 28 Feb 2024 11:38:31 +0100 Subject: [PATCH] Use context manager for reading Excel file (#818) * Use context manager for reading Excel file * Update RELEASE_NOTES.md * Apply black * Update pytest workflow * Pin ixmp4 to fix failing test --- .github/workflows/pytest.yml | 6 +++--- RELEASE_NOTES.md | 1 + pyam/utils.py | 26 ++++++++++++-------------- setup.cfg | 2 +- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 7e71cba34..31c86da25 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -25,12 +25,12 @@ jobs: runs-on: ${{ matrix.os }} name: ${{ matrix.os }} py${{ matrix.python-version }} - + steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 551372259..bbe3f1288 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,5 +1,6 @@ # Next release +- [#818](https://github.com/IAMconsortium/pyam/pull/818) Use context manager for reading Excel file - [#813](https://github.com/IAMconsortium/pyam/pull/813) Fix a corner case in region-aggregation with missing data - [#797](https://github.com/IAMconsortium/pyam/pull/797) Add `to_ixmp4()` method to write to an **ixmp4** platform diff --git a/pyam/utils.py b/pyam/utils.py index 6bfefe616..fdba803d0 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: @@ -116,18 +114,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): diff --git a/setup.cfg b/setup.cfg index 4fd25c099..8202486c2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,7 +27,7 @@ python_requires = >=3.10, <3.12 # Please also add a section "Dependency changes" to the release notes install_requires = iam-units >= 2020.4.21 - ixmp4 >= 0.6.0 + ixmp4 == 0.6.0 numpy >= 1.23.0, < 1.24 # requests included via ixmp4 # httpx[http2] included via ixmp4