Skip to content

BUG: IntervalArray.__cmp__(pd.NA) #44830

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 3 commits into from
Dec 11, 2021
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
5 changes: 5 additions & 0 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
is_unsigned_integer_dtype,
pandas_dtype,
)
from pandas.core.dtypes.dtypes import IntervalDtype

import pandas as pd
from pandas import (
Expand Down Expand Up @@ -282,6 +283,10 @@ def to_array(obj):
return DatetimeArray._from_sequence(obj)
elif is_timedelta64_dtype(dtype):
return TimedeltaArray._from_sequence(obj)
elif isinstance(obj, pd.core.arrays.BooleanArray):
return obj
elif isinstance(dtype, IntervalDtype):
return pd.core.arrays.IntervalArray(obj)
Copy link
Contributor

Choose a reason for hiding this comment

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

can you use is_interval_dtype to be consistent

else:
return np.array(obj)

Expand Down
10 changes: 9 additions & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,13 @@ def _cmp_method(self, other, op):
other = pd_array(other)
elif not isinstance(other, Interval):
# non-interval scalar -> no matches
if other is NA:
# GH#31882
from pandas.core.arrays import BooleanArray

arr = np.empty(self.shape, dtype=bool)
mask = np.ones(self.shape, dtype=bool)
return BooleanArray(arr, mask)
return invalid_comparison(self, other, op)

# determine the dtype of the elements we want to compare
Expand Down Expand Up @@ -743,7 +750,8 @@ def _cmp_method(self, other, op):
if obj is NA:
# comparison with np.nan returns NA
# github.com/pandas-dev/pandas/pull/37124#discussion_r509095092
result[i] = op is operator.ne
result = result.astype(object)
result[i] = NA
else:
raise
return result
Expand Down
54 changes: 36 additions & 18 deletions pandas/tests/arithmetic/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
timedelta_range,
)
import pandas._testing as tm
from pandas.core.arrays import IntervalArray
from pandas.core.arrays import (
BooleanArray,
IntervalArray,
)
from pandas.tests.arithmetic.common import get_upcast_box


@pytest.fixture(
Expand Down Expand Up @@ -129,18 +133,37 @@ def test_compare_scalar_interval_mixed_closed(self, op, closed, other_closed):
expected = self.elementwise_comparison(op, interval_array, other)
tm.assert_numpy_array_equal(result, expected)

def test_compare_scalar_na(self, op, interval_array, nulls_fixture, request):
result = op(interval_array, nulls_fixture)
expected = self.elementwise_comparison(op, interval_array, nulls_fixture)
def test_compare_scalar_na(
self, op, interval_array, nulls_fixture, box_with_array, request
):
box = box_with_array

if box is pd.DataFrame:
if interval_array.dtype.subtype.kind not in "iuf":
mark = pytest.mark.xfail(
reason="raises on DataFrame.transpose (would be fixed by EA2D)"
)
request.node.add_marker(mark)

obj = tm.box_expected(interval_array, box)
result = op(obj, nulls_fixture)

if nulls_fixture is pd.NA:
# GH#31882
exp = np.ones(interval_array.shape, dtype=bool)
expected = BooleanArray(exp, exp)
else:
expected = self.elementwise_comparison(op, interval_array, nulls_fixture)

if not (box is Index and nulls_fixture is pd.NA):
# don't cast expected from BooleanArray to ndarray[object]
xbox = get_upcast_box(obj, nulls_fixture, True)
expected = tm.box_expected(expected, xbox)

if nulls_fixture is pd.NA and interval_array.dtype.subtype != "int64":
mark = pytest.mark.xfail(
raises=AssertionError,
reason="broken for non-integer IntervalArray; see GH 31882",
)
request.node.add_marker(mark)
tm.assert_equal(result, expected)

tm.assert_numpy_array_equal(result, expected)
rev = op(nulls_fixture, obj)
tm.assert_equal(rev, expected)

@pytest.mark.parametrize(
"other",
Expand Down Expand Up @@ -214,17 +237,12 @@ def test_compare_list_like_object(self, op, interval_array, other):
expected = self.elementwise_comparison(op, interval_array, other)
tm.assert_numpy_array_equal(result, expected)

def test_compare_list_like_nan(self, op, interval_array, nulls_fixture, request):
def test_compare_list_like_nan(self, op, interval_array, nulls_fixture):
other = [nulls_fixture] * 4
result = op(interval_array, other)
expected = self.elementwise_comparison(op, interval_array, other)

if nulls_fixture is pd.NA and interval_array.dtype.subtype != "i8":
reason = "broken for non-integer IntervalArray; see GH 31882"
mark = pytest.mark.xfail(raises=AssertionError, reason=reason)
request.node.add_marker(mark)

tm.assert_numpy_array_equal(result, expected)
tm.assert_equal(result, expected)

@pytest.mark.parametrize(
"other",
Expand Down