Skip to content

Commit b3552c5

Browse files
authored
REGR: assert_index_equal raising with non matching pd.NA (#48608)
* REGR: assert_index_equal raising with non matching pd.NA * Fix gh ref * Fix * Fix ci * Move whatsnew * Fix categorical
1 parent 2a6c6a2 commit b3552c5

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

doc/source/whatsnew/v1.5.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Bug fixes
8686
~~~~~~~~~
8787
- Bug in :meth:`Series.__getitem__` not falling back to positional for integer keys and boolean :class:`Index` (:issue:`48653`)
8888
- Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`48667`)
89+
- Bug in :func:`assert_index_equal` for extension arrays with non matching ``NA`` raising ``ValueError`` (:issue:`48608`)
8990
- Bug in :meth:`DataFrame.pivot_table` raising unexpected ``FutureWarning`` when setting datetime column as index (:issue:`48683`)
9091
-
9192

pandas/_libs/testing.pyx

+4
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ cpdef assert_almost_equal(a, b,
182182
# nan / None comparison
183183
return True
184184

185+
if isna(a) and not isna(b) or not isna(a) and isna(b):
186+
# boolean value of pd.NA is ambigous
187+
raise AssertionError(f"{a} != {b}")
188+
185189
if a == b:
186190
# object comparison
187191
return True

pandas/_testing/asserters.py

+3
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ def _get_ilevel_values(index, level):
398398
if not left.equals(right):
399399
mismatch = left._values != right._values
400400

401+
if is_extension_array_dtype(mismatch):
402+
mismatch = cast("ExtensionArray", mismatch).fillna(True)
403+
401404
diff = np.sum(mismatch.astype(int)) * 100.0 / len(left)
402405
msg = f"{obj} values are different ({np.round(diff, 5)} %)"
403406
raise_assert_detail(obj, msg, left, right)

pandas/tests/util/test_assert_index_equal.py

+12
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,15 @@ def test_assert_index_equal_object_ints_order_false():
280280
idx1 = Index([1, 3], dtype="object")
281281
idx2 = Index([3, 1], dtype="object")
282282
tm.assert_index_equal(idx1, idx2, check_order=False)
283+
284+
285+
@pytest.mark.parametrize("check_categorical", [True, False])
286+
@pytest.mark.parametrize("check_names", [True, False])
287+
def test_assert_ea_index_equal_non_matching_na(check_names, check_categorical):
288+
# GH#48608
289+
idx1 = Index([1, 2], dtype="Int64")
290+
idx2 = Index([1, NA], dtype="Int64")
291+
with pytest.raises(AssertionError, match="50.0 %"):
292+
tm.assert_index_equal(
293+
idx1, idx2, check_names=check_names, check_categorical=check_categorical
294+
)

0 commit comments

Comments
 (0)