Skip to content

Commit d7f30b4

Browse files
authored
REGR: Fix comparison broadcasting over array of Intervals (#35938)
1 parent bcb9e1b commit d7f30b4

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

doc/source/whatsnew/v1.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fixed regressions
1717
- Regression in :meth:`DatetimeIndex.intersection` incorrectly raising ``AssertionError`` when intersecting against a list (:issue:`35876`)
1818
- Fix regression in updating a column inplace (e.g. using ``df['col'].fillna(.., inplace=True)``) (:issue:`35731`)
1919
- Performance regression for :meth:`RangeIndex.format` (:issue:`35712`)
20+
- Regression in :meth:`DataFrame.replace` where a ``TypeError`` would be raised when attempting to replace elements of type :class:`Interval` (:issue:`35931`)
2021
-
2122

2223

pandas/_libs/interval.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ cdef class Interval(IntervalMixin):
358358
self_tuple = (self.left, self.right, self.closed)
359359
other_tuple = (other.left, other.right, other.closed)
360360
return PyObject_RichCompare(self_tuple, other_tuple, op)
361+
elif util.is_array(other):
362+
return np.array(
363+
[PyObject_RichCompare(self, x, op) for x in other],
364+
dtype=bool,
365+
)
361366

362367
return NotImplemented
363368

pandas/tests/frame/methods/test_replace.py

+7
Original file line numberDiff line numberDiff line change
@@ -1581,3 +1581,10 @@ def test_replace_with_compiled_regex(self):
15811581
result = df.replace({regex: "z"}, regex=True)
15821582
expected = pd.DataFrame(["z", "b", "c"])
15831583
tm.assert_frame_equal(result, expected)
1584+
1585+
def test_replace_intervals(self):
1586+
# https://github.com/pandas-dev/pandas/issues/35931
1587+
df = pd.DataFrame({"a": [pd.Interval(0, 1), pd.Interval(0, 1)]})
1588+
result = df.replace({"a": {pd.Interval(0, 1): "x"}})
1589+
expected = pd.DataFrame({"a": ["x", "x"]})
1590+
tm.assert_frame_equal(result, expected)

pandas/tests/scalar/interval/test_arithmetic.py

+12
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ def test_numeric_interval_add_timedelta_raises(interval, delta):
4545

4646
with pytest.raises((TypeError, ValueError), match=msg):
4747
delta + interval
48+
49+
50+
@pytest.mark.parametrize("klass", [timedelta, np.timedelta64, Timedelta])
51+
def test_timdelta_add_timestamp_interval(klass):
52+
delta = klass(0)
53+
expected = Interval(Timestamp("2020-01-01"), Timestamp("2020-02-01"))
54+
55+
result = delta + expected
56+
assert result == expected
57+
58+
result = expected + delta
59+
assert result == expected

pandas/tests/scalar/interval/test_interval.py

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

44
from pandas import Interval, Period, Timedelta, Timestamp
5+
import pandas._testing as tm
56
import pandas.core.common as com
67

78

@@ -267,3 +268,11 @@ def test_constructor_errors_tz(self, tz_left, tz_right):
267268
msg = "left and right must have the same time zone"
268269
with pytest.raises(error, match=msg):
269270
Interval(left, right)
271+
272+
def test_equality_comparison_broadcasts_over_array(self):
273+
# https://github.com/pandas-dev/pandas/issues/35931
274+
interval = Interval(0, 1)
275+
arr = np.array([interval, interval])
276+
result = interval == arr
277+
expected = np.array([True, True])
278+
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)