Skip to content

Commit

Permalink
fix compress_memberships bug, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierBinette committed Nov 17, 2023
1 parent bac68df commit 79f1974
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions er_evaluation/data_structures/_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def compress_memberships(*memberships):
Name: 0, dtype: int8
"""
compressed = pd.concat(memberships, axis=1)
compressed.index = np.where(compressed.index.isna(), np.nan, pd.Categorical(compressed.index).codes)
compressed.index = pd.Categorical(compressed.index).codes
for col in compressed.columns:
compressed[col] = pd.Categorical(compressed[col]).codes
compressed[col] = np.where(compressed[col].isna(), np.nan, pd.Categorical(compressed[col]).codes)

return [compressed[col] for col in compressed.columns]

Expand Down
12 changes: 12 additions & 0 deletions tests/test_data_structures/test_compress_memberships.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pandas as pd

from er_evaluation.data_structures import compress_memberships

def test_keep_na_values_in_index():
series1 = pd.Series(index=[-1, 0, 4, 7], data=[pd.NA, 1, 2, 3])
series2 = pd.Series(index=[1, 0, 4, 8], data=[1, pd.NA, 2, 3])
cs1, cs2 = compress_memberships(series1, series2)

assert cs1.isna().sum() == 3
assert cs2.isna().sum() == 3

0 comments on commit 79f1974

Please sign in to comment.