Skip to content

Commit 9080d30

Browse files
authored
CLN: avoid ndim checks in _libs (#34107)
1 parent afe7f1d commit 9080d30

File tree

4 files changed

+17
-25
lines changed

4 files changed

+17
-25
lines changed

pandas/_libs/interval.pyx

+2-15
Original file line numberDiff line numberDiff line change
@@ -355,25 +355,12 @@ cdef class Interval(IntervalMixin):
355355
(key < self.right if self.open_right else key <= self.right))
356356

357357
def __richcmp__(self, other, op: int):
358-
if hasattr(other, 'ndim'):
359-
# let numpy (or IntervalIndex) handle vectorization
360-
return NotImplemented
361-
362-
if _interval_like(other):
358+
if isinstance(other, Interval):
363359
self_tuple = (self.left, self.right, self.closed)
364360
other_tuple = (other.left, other.right, other.closed)
365361
return PyObject_RichCompare(self_tuple, other_tuple, op)
366362

367-
# nb. could just return NotImplemented now, but handling this
368-
# explicitly allows us to opt into the Python 3 behavior, even on
369-
# Python 2.
370-
if op == Py_EQ or op == Py_NE:
371-
return NotImplemented
372-
else:
373-
name = type(self).__name__
374-
other = type(other).__name__
375-
op_str = {Py_LT: '<', Py_LE: '<=', Py_GT: '>', Py_GE: '>='}[op]
376-
raise TypeError(f"unorderable types: {name}() {op_str} {other}()")
363+
return NotImplemented
377364

378365
def __reduce__(self):
379366
args = (self.left, self.right, self.closed)

pandas/_libs/tslibs/timedeltas.pyx

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from cpython.object cimport Py_NE, Py_EQ, PyObject_RichCompare
66

77
import numpy as np
88
cimport numpy as cnp
9-
from numpy cimport int64_t
9+
from numpy cimport int64_t, ndarray
1010
cnp.import_array()
1111

1212
from cpython.datetime cimport (timedelta,
@@ -1469,7 +1469,7 @@ cdef _rfloordiv(int64_t value, right):
14691469

14701470
cdef _broadcast_floordiv_td64(
14711471
int64_t value,
1472-
object other,
1472+
ndarray other,
14731473
object (*operation)(int64_t value, object right)
14741474
):
14751475
"""
@@ -1487,13 +1487,11 @@ cdef _broadcast_floordiv_td64(
14871487
result : varies based on `other`
14881488
"""
14891489
# assumes other.dtype.kind == 'm', i.e. other is timedelta-like
1490-
cdef:
1491-
int ndim = getattr(other, 'ndim', -1)
14921490

14931491
# We need to watch out for np.timedelta64('NaT').
14941492
mask = other.view('i8') == NPY_NAT
14951493

1496-
if ndim == 0:
1494+
if other.ndim == 0:
14971495
if mask:
14981496
return np.nan
14991497

pandas/tests/indexes/interval/test_interval.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,17 @@ def test_comparison(self):
568568
actual = self.index == self.index.left
569569
tm.assert_numpy_array_equal(actual, np.array([False, False]))
570570

571-
with pytest.raises(TypeError, match="unorderable types"):
571+
msg = (
572+
"not supported between instances of 'int' and "
573+
"'pandas._libs.interval.Interval'"
574+
)
575+
with pytest.raises(TypeError, match=msg):
572576
self.index > 0
573-
with pytest.raises(TypeError, match="unorderable types"):
577+
with pytest.raises(TypeError, match=msg):
574578
self.index <= 0
575-
msg = r"unorderable types: Interval\(\) > int\(\)"
576579
with pytest.raises(TypeError, match=msg):
577580
self.index > np.arange(2)
581+
578582
msg = "Lengths must match"
579583
with pytest.raises(ValueError, match=msg):
580584
self.index > np.arange(3)
@@ -894,6 +898,6 @@ def test_searchsorted_different_argument_classes(klass):
894898
)
895899
def test_searchsorted_invalid_argument(arg):
896900
values = IntervalIndex([Interval(0, 1), Interval(1, 2)])
897-
msg = "unorderable types"
901+
msg = "'<' not supported between instances of 'pandas._libs.interval.Interval' and "
898902
with pytest.raises(TypeError, match=msg):
899903
values.searchsorted(arg)

pandas/tests/scalar/interval/test_interval.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ def test_equal(self):
4949
assert Interval(0, 1) != 0
5050

5151
def test_comparison(self):
52-
msg = "unorderable types"
52+
msg = (
53+
"'<' not supported between instances of "
54+
"'pandas._libs.interval.Interval' and 'int'"
55+
)
5356
with pytest.raises(TypeError, match=msg):
5457
Interval(0, 1) < 2
5558

0 commit comments

Comments
 (0)