Skip to content

Prevent RecursionError on Timestamp comparison against object array #18829

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 2 commits into from
Dec 19, 2017
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
@@ -346,3 +346,4 @@ Other
^^^^^

- Improved error message when attempting to use a Python keyword as an identifier in a ``numexpr`` backed query (:issue:`18221`)
- Bug in :class:`Timestamp` where comparison with an array of ``Timestamp`` objects would result in a ``RecursionError`` (:issue:`15183`)
5 changes: 4 additions & 1 deletion pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ from cpython.datetime cimport (datetime,
PyDateTime_IMPORT

from util cimport (is_datetime64_object, is_timedelta64_object,
is_integer_object, is_string_object,
is_integer_object, is_string_object, is_array,
INT64_MAX)

cimport ccalendar
@@ -108,6 +108,9 @@ cdef class _Timestamp(datetime):
raise TypeError('Cannot compare type %r with type %r' %
(type(self).__name__,
type(other).__name__))
elif is_array(other):
# avoid recursion error GH#15183
return PyObject_RichCompare(np.array([self]), other, op)
return PyObject_RichCompare(other, self, reverse_ops[op])
else:
if op == Py_EQ:
21 changes: 21 additions & 0 deletions pandas/tests/scalar/test_timedelta.py
Original file line number Diff line number Diff line change
@@ -163,6 +163,27 @@ def test_binary_ops_with_timedelta(self):
pytest.raises(TypeError, lambda: td * td)


class TestTimedeltaComparison(object):
def test_comparison_object_array(self):
# analogous to GH#15183
td = Timedelta('2 days')
other = Timedelta('3 hours')

arr = np.array([other, td], dtype=object)
res = arr == td
expected = np.array([False, True], dtype=bool)
assert (res == expected).all()

# 2D case
arr = np.array([[other, td],
[td, other]],
dtype=object)
res = arr != td
expected = np.array([[True, False], [False, True]], dtype=bool)
assert res.shape == expected.shape
assert (res == expected).all()


class TestTimedeltas(object):
_multiprocess_can_split_ = True

25 changes: 25 additions & 0 deletions pandas/tests/scalar/test_timestamp.py
Original file line number Diff line number Diff line change
@@ -969,6 +969,31 @@ def test_timestamp(self):


class TestTimestampComparison(object):
def test_comparison_object_array(self):
# GH#15183
ts = Timestamp('2011-01-03 00:00:00-0500', tz='US/Eastern')
other = Timestamp('2011-01-01 00:00:00-0500', tz='US/Eastern')
naive = Timestamp('2011-01-01 00:00:00')

arr = np.array([other, ts], dtype=object)
res = arr == ts
expected = np.array([False, True], dtype=bool)
assert (res == expected).all()

# 2D case
arr = np.array([[other, ts],
[ts, other]],
dtype=object)
res = arr != ts
expected = np.array([[True, False], [False, True]], dtype=bool)
assert res.shape == expected.shape
assert (res == expected).all()

# tzaware mismatch
arr = np.array([naive], dtype=object)
with pytest.raises(TypeError):
arr < ts

def test_comparison(self):
# 5-18-2012 00:00:00.000
stamp = long(1337299200000000000)