Skip to content

Commit

Permalink
Use context manager for reading Excel file (#818)
Browse files Browse the repository at this point in the history
* Use context manager for reading Excel file

* Update RELEASE_NOTES.md

* Apply black

* Update pytest workflow

* Pin ixmp4 to fix failing test
  • Loading branch information
phackstock authored Feb 28, 2024
1 parent 0762055 commit 3d13de4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand Down
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
26 changes: 12 additions & 14 deletions pyam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3d13de4

Please sign in to comment.