Skip to content

BUG: Index.equals raising with index of tuples that contain NA #48446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Indexing

Missing
^^^^^^^
-
- Bug in :meth:`Index.equals` raising ``TypeError` when :class:`Index` consists of tuples that contain ``NA`` (:issue:`48446`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this is not rendering correctly due to unbalanced backticks around TypeError.

-

MultiIndex
Expand Down
9 changes: 8 additions & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ from cython cimport (
floating,
)

from pandas._libs.missing import check_na_tuples_nonequal

import_datetime()

import numpy as np
Expand Down Expand Up @@ -636,7 +638,7 @@ def array_equivalent_object(left: object[:], right: object[:]) -> bool:
or is_matching_na(x, y, nan_matches_none=True)
):
return False
except ValueError:
except (ValueError, TypeError):
# Avoid raising ValueError when comparing Numpy arrays to other types
if cnp.PyArray_IsAnyScalar(x) != cnp.PyArray_IsAnyScalar(y):
# Only compare scalars to scalars and non-scalars to non-scalars
Expand All @@ -645,7 +647,12 @@ def array_equivalent_object(left: object[:], right: object[:]) -> bool:
and not (isinstance(x, type(y)) or isinstance(y, type(x)))):
# Check if non-scalars have the same type
return False
elif check_na_tuples_nonequal(x, y):
# We have tuples where one Side has a NA and the other side does not
# Only condition we may end up with a TypeError
return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment that this is the only/main we we expect to get a TypeError

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

raise

return True


Expand Down
1 change: 1 addition & 0 deletions pandas/_libs/missing.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from numpy cimport (


cpdef bint is_matching_na(object left, object right, bint nan_matches_none=*)
cpdef bint check_na_tuples_nonequal(object left, object right)

cpdef bint checknull(object val, bint inf_as_na=*)
cpdef ndarray[uint8_t] isnaobj(ndarray arr, bint inf_as_na=*)
Expand Down
31 changes: 31 additions & 0 deletions pandas/_libs/missing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,37 @@ cdef:
type cDecimal = Decimal # for faster isinstance checks


cpdef bint check_na_tuples_nonequal(object left, object right):
"""
When we have NA in one of the tuples but not the other we have to check here,
because our regular checks fail before with ambigous boolean value.

Parameters
----------
left: Any
right: Any

Returns
-------
True if we are dealing with tuples that have NA on one side and non NA on
the other side.

"""
if not isinstance(left, tuple) or not isinstance(right, tuple):
return False

if len(left) != len(right):
return False

for left_element, right_element in zip(left, right):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this assert that len(left) == len(right)? Guessing this is assumed upstream but technically (1, NA) and (1,) would return False

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. I think we could end up here with weird combinations, so better to be a bit more explicit

if left_element is C_NA and right_element is not C_NA:
return True
elif right_element is C_NA and left_element is not C_NA:
return True

return False


cpdef bint is_matching_na(object left, object right, bint nan_matches_none=False):
"""
Check if two scalars are both NA of matching types.
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/dtypes/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,23 @@ def test_array_equivalent_nested():
assert not array_equivalent(left, right, strict_nan=True)


def test_array_equivalent_index_with_tuples():
# GH#48446
idx1 = pd.Index(np.array([(pd.NA, 4), (1, 1)], dtype="object"))
idx2 = pd.Index(np.array([(1, 1), (pd.NA, 4)], dtype="object"))
assert not array_equivalent(idx1, idx2)
assert not idx1.equals(idx2)
assert not array_equivalent(idx2, idx1)
assert not idx2.equals(idx1)

idx1 = pd.Index(np.array([(4, pd.NA), (1, 1)], dtype="object"))
idx2 = pd.Index(np.array([(1, 1), (4, pd.NA)], dtype="object"))
assert not array_equivalent(idx1, idx2)
assert not idx1.equals(idx2)
assert not array_equivalent(idx2, idx1)
assert not idx2.equals(idx1)


@pytest.mark.parametrize(
"dtype, na_value",
[
Expand Down