Skip to content

Commit

Permalink
Bugfix for non-empty invisible header in 'meta' sheet (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann authored Mar 18, 2021
1 parent 5ddb3e9 commit a9e93e0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ starting with `data` will be parsed for timeseries data.

## Individual updates

- [#508](https://github.com/IAMconsortium/pyam/pull/508) Bugfix for non-empty but invisible header and no rows in 'meta' sheet
- [#502](https://github.com/IAMconsortium/pyam/pull/502) Switch to Black code style
- [#499](https://github.com/IAMconsortium/pyam/pull/499) Implement `order` feature in line plot
- [#497](https://github.com/IAMconsortium/pyam/pull/497) Add a module for reading data from the UNFCCC Data Inventory
Expand Down
12 changes: 7 additions & 5 deletions pyam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,13 @@ def read_pandas(path, sheet_name="data*", *args, **kwargs):
df = pd.read_excel(path, *args, **kwargs)

# remove unnamed and empty columns, and rows were all values are nan
empty_cols = [
c
for c in df.columns
if str(c).startswith("Unnamed: ") and all(np.isnan(df[c]))
]
def is_empty(name, s):
if str(name).startswith("Unnamed: "):
if len(s) == 0 or all(np.isnan(s)):
return True
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")


Expand Down
Binary file added tests/data/empty_meta_sheet.xlsx
Binary file not shown.
9 changes: 8 additions & 1 deletion tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def test_load_meta_wrong_index(test_df_year, tmpdir):


def test_load_meta_empty_rows(test_df_year, tmpdir):
"""Loading empty meta table (columns but no rows) from an Excel file"""
"""Loading empty meta table (columns but no rows) from xlsx file"""
exp = test_df_year.copy() # loading empty file has no effect

# write empty meta frame to file, then load to the IamDataFrame
Expand All @@ -182,6 +182,13 @@ def test_load_meta_empty_rows(test_df_year, tmpdir):
assert_iamframe_equal(test_df_year, exp)


def test_load_meta_empty(test_pd_df):
"""Initializing from xlsx where 'meta' has no rows and non-empty invisible header"""
obs = IamDataFrame(TEST_DATA_DIR / "empty_meta_sheet.xlsx")
exp = IamDataFrame(test_pd_df)
assert_iamframe_equal(obs, exp)


def test_load_ssp_database_downloaded_file(test_pd_df):
exp = IamDataFrame(test_pd_df).filter(**FILTER_ARGS).as_pandas()
file = TEST_DATA_DIR / "test_SSP_database_raw_download.xlsx"
Expand Down

0 comments on commit a9e93e0

Please sign in to comment.