Skip to content

BUG: GH24011 - Rich comparisons of Timestamps now return NotImplemented #24021

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 16 commits into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 1 addition & 6 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,7 @@ cdef class _Timestamp(datetime):
return PyObject_RichCompare(np.array([self]), other, op)
return PyObject_RichCompare(other, self, reverse_ops[op])
else:
if op == Py_EQ:
return False
elif op == Py_NE:
return True
raise TypeError('Cannot compare type %r with type %r' %
(type(self).__name__, type(other).__name__))
return NotImplemented

self._assert_tzawareness_compat(other)
return cmp_scalar(self.value, ots.value, op)
Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/tslibs/test_tslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,33 @@ def test_normalize_date():

actual = tslibs.normalize_date(value)
assert actual == datetime(2007, 10, 1)


def test_rich_comparison_with_unsupported_type():
# See https://github.com/pandas-dev/pandas/issues/24011

class Inf:
def __lt__(self, o):
return False

def __le__(self, o):
return isinstance(o, Inf)

def __gt__(self, o):
return not isinstance(o, Inf)

def __ge__(self, o):
return True

def __eq__(self, o):
return isinstance(o, Inf)

timestamp = tslibs.Timestamp('2018-11-30')

# Comparison works if compared in *that* order, because
# magic method is called on Inf
assert Inf() > timestamp
assert not (Inf() < timestamp)

# ... but used to not work when magic method is called on Timestamp
assert timestamp < Inf()