Skip to content

Commit 9c3bc88

Browse files
Added case for no kwargs match
1 parent fa63231 commit 9c3bc88

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pymrio/tools/ioutil.py

+8
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,7 @@ def _index_regex_matcher(_dfs_idx, _method, _find_all=None, **kwargs):
950950
else:
951951
return _dfs_idx[found]
952952

953+
at_least_one_valid = False
953954
for key, value in kwargs.items():
954955
try:
955956
if type(_dfs_idx) in [pd.DataFrame, pd.Series]:
@@ -962,7 +963,14 @@ def _index_regex_matcher(_dfs_idx, _method, _find_all=None, **kwargs):
962963
"pd.DataFrame, pd.Series, pd.Index or pd.MultiIndex"
963964
)
964965
_dfs_idx = _dfs_idx[fun(value, case=True, flags=0, na=False)]
966+
at_least_one_valid = True
965967
except KeyError:
966968
pass
967969

970+
if not at_least_one_valid:
971+
if type(_dfs_idx) in [pd.DataFrame, pd.Series]:
972+
_dfs_idx = pd.DataFrame(index=[], columns=_dfs_idx.columns)
973+
elif type(_dfs_idx) in [pd.Index, pd.MultiIndex]:
974+
_dfs_idx = pd.Index([])
975+
968976
return _dfs_idx

tests/test_util.py

+11
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,14 @@ def test_util_regex():
339339
# 5. test wrong input
340340
with pytest.raises(ValueError):
341341
index_fullmatch("foo", region="a.*", sector=".*b.*", not_present_column="abc")
342+
343+
# 6. test with all kwargs not present
344+
345+
df_some_match = index_match(test_df, region="a.*", sector=".*b.*", not_present_column="abc")
346+
assert df_some_match.index.get_level_values("region").unique() == ["a1"]
347+
assert df_some_match.index.get_level_values("sector").unique() == ["bb"]
348+
349+
df_none_match = index_match(test_df, not_present_column="abc")
350+
df_none_match_index = index_contains(test_index, not_present_column="abc")
351+
assert len(df_none_match) == 0
352+
assert len(df_none_match_index) == 0

0 commit comments

Comments
 (0)