Skip to content

Commit 160e7f2

Browse files
committed
Merge pull request pandas-dev#11957 from kawochen/BUG-FIX-11835
BUG: GH11835 where comparison of Timedelta array caused infinite loop
2 parents cbd7f18 + 5866b2c commit 160e7f2

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

doc/source/whatsnew/v0.18.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ Bug Fixes
476476

477477
- Bug in ``.style.bar`` may not rendered properly using specific browser (:issue:`11678`)
478478

479+
- Bug in rich comparison of ``Timedelta`` with a ``numpy.array`` of ``Timedelta``s that caused an infinite recursion (:issue:`11835`)
479480

480481
- Bug in ``df.replace`` while replacing value in mixed dtype ``Dataframe`` (:issue:`11698`)
481482

pandas/tseries/tests/test_timedeltas.py

+8
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,14 @@ def test_compare_timedelta_series(self):
363363
expected = pd.Series([False, True])
364364
tm.assert_series_equal(actual, expected)
365365

366+
def test_compare_timedelta_ndarray(self):
367+
# GH11835
368+
periods = [Timedelta('0 days 01:00:00'), Timedelta('0 days 01:00:00')]
369+
arr = np.array(periods)
370+
result = arr[0] > arr
371+
expected = np.array([False, False])
372+
self.assert_numpy_array_equal(result, expected)
373+
366374
def test_ops_notimplemented(self):
367375
class Other:
368376
pass

pandas/tslib.pyx

+2
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,8 @@ cdef class _Timedelta(timedelta):
21842184
raise TypeError('Cannot compare type %r with type %r' %
21852185
(type(self).__name__,
21862186
type(other).__name__))
2187+
if isinstance(other, np.ndarray):
2188+
return PyObject_RichCompare(np.array([self]), other, op)
21872189
return PyObject_RichCompare(other, self, _reverse_ops[op])
21882190
else:
21892191
if op == Py_EQ:

0 commit comments

Comments
 (0)