Skip to content

Commit 35e1c23

Browse files
phoflmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#48608: REGR: assert_index_equal raising with non matching pd.NA
1 parent 2dfbe0c commit 35e1c23

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
@@ -84,6 +84,7 @@ Fixed regressions
8484
Bug fixes
8585
~~~~~~~~~
8686
- Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`48667`)
87+
- Bug in :func:`assert_index_equal` for extension arrays with non matching ``NA`` raising ``ValueError`` (:issue:`48608`)
8788
- Bug in :meth:`DataFrame.pivot_table` raising unexpected ``FutureWarning`` when setting datetime column as index (:issue:`48683`)
8889
-
8990

pandas/_libs/testing.pyx

+4
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ cpdef assert_almost_equal(a, b,
179179
# nan / None comparison
180180
return True
181181

182+
if isna(a) and not isna(b) or not isna(a) and isna(b):
183+
# boolean value of pd.NA is ambigous
184+
raise AssertionError(f"{a} != {b}")
185+
182186
if a == b:
183187
# object comparison
184188
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
@@ -264,3 +264,15 @@ def test_assert_index_equal_object_ints_order_false():
264264
idx1 = Index([1, 3], dtype="object")
265265
idx2 = Index([3, 1], dtype="object")
266266
tm.assert_index_equal(idx1, idx2, check_order=False)
267+
268+
269+
@pytest.mark.parametrize("check_categorical", [True, False])
270+
@pytest.mark.parametrize("check_names", [True, False])
271+
def test_assert_ea_index_equal_non_matching_na(check_names, check_categorical):
272+
# GH#48608
273+
idx1 = Index([1, 2], dtype="Int64")
274+
idx2 = Index([1, NA], dtype="Int64")
275+
with pytest.raises(AssertionError, match="50.0 %"):
276+
tm.assert_index_equal(
277+
idx1, idx2, check_names=check_names, check_categorical=check_categorical
278+
)

0 commit comments

Comments
 (0)