Skip to content

Commit ae20b63

Browse files
Backport PR #48608 on branch 1.5.x (REGR: assert_index_equal raising with non matching pd.NA) (#48800)
* Backport PR #48608: REGR: assert_index_equal raising with non matching pd.NA Co-authored-by: Patrick Hoefler <[email protected]>
1 parent ff7280b commit ae20b63

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

doc/source/whatsnew/v1.5.1.rst

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

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

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33

44
from pandas import (
5+
NA,
56
Categorical,
67
CategoricalIndex,
78
Index,
@@ -264,3 +265,15 @@ def test_assert_index_equal_object_ints_order_false():
264265
idx1 = Index([1, 3], dtype="object")
265266
idx2 = Index([3, 1], dtype="object")
266267
tm.assert_index_equal(idx1, idx2, check_order=False)
268+
269+
270+
@pytest.mark.parametrize("check_categorical", [True, False])
271+
@pytest.mark.parametrize("check_names", [True, False])
272+
def test_assert_ea_index_equal_non_matching_na(check_names, check_categorical):
273+
# GH#48608
274+
idx1 = Index([1, 2], dtype="Int64")
275+
idx2 = Index([1, NA], dtype="Int64")
276+
with pytest.raises(AssertionError, match="50.0 %"):
277+
tm.assert_index_equal(
278+
idx1, idx2, check_names=check_names, check_categorical=check_categorical
279+
)

0 commit comments

Comments
 (0)