Skip to content

Bugfix timedelta notimplemented eq #21394

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
Show file tree
Hide file tree
Changes from 6 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.23.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Data-type specific

- Bug in :meth:`Series.str.replace()` where the method throws `TypeError` on Python 3.5.2 (:issue: `21078`)
- Bug in :class:`Timedelta`: where passing a float with a unit would prematurely round the float precision (:issue: `14156`)
- Bug in :class:`Timedelta`: Comparison with unknown types now return NotImplemented as typically expected (:issue: `20829`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to 0.24

- Bug in :func:`pandas.testing.assert_index_equal` which raised ``AssertionError`` incorrectly, when comparing two :class:`CategoricalIndex` objects with param ``check_categorical=False`` (:issue:`19776`)

Sparse
Expand Down
18 changes: 2 additions & 16 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -680,26 +680,12 @@ cdef class _Timedelta(timedelta):
if is_timedelta64_object(other):
other = Timedelta(other)
else:
if op == Py_EQ:
return False
elif op == Py_NE:
return True

# only allow ==, != ops
raise TypeError('Cannot compare type {!r} with type ' \
'{!r}'.format(type(self).__name__,
type(other).__name__))
return NotImplemented
if util.is_array(other):
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}'.format(type(self).__name__,
type(other).__name__))
return NotImplemented

return cmp_scalar(self.value, ots.value, op)

Expand Down
55 changes: 53 additions & 2 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" test the scalar Timedelta """
import sys
import pytest

import numpy as np
Expand Down Expand Up @@ -42,8 +43,10 @@ def test_ops_error_str(self):
with pytest.raises(TypeError):
left + right

with pytest.raises(TypeError):
left > right
# GH 20829: python 2 comparison naturally does not raise TypeError
if sys.version_info >= (3, 0):
with pytest.raises(TypeError):
left > right

assert not left == right
assert left != right
Expand Down Expand Up @@ -103,6 +106,54 @@ def test_compare_timedelta_ndarray(self):
expected = np.array([False, False])
tm.assert_numpy_array_equal(result, expected)

def test_custom_comparison_object(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_compare_custom_object

# GH20829
class CustomClass(object):

def __init__(self, cmp_result=None):
self.cmp_result = cmp_result

def generic_result(self):
if self.cmp_result is None:
return NotImplemented
else:
return self.cmp_result

def __eq__(self, other):
return self.generic_result()

def __gt__(self, other):
return self.generic_result()

t = Timedelta('1s')

assert not (t == "string")
assert not (t == 1)
assert not (t == CustomClass())
assert not (t == CustomClass(cmp_result=False))

assert t < CustomClass(cmp_result=True)
assert not (t < CustomClass(cmp_result=False))

assert t == CustomClass(cmp_result=True)

@pytest.mark.parametrize("val", [
"string", 1])
def test_raise_comparisons_unknown_types(self, val):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_compare_unknown_type

# GH20829
t = Timedelta('1s')
if sys.version_info >= (3, 0):
# python 2 does not raises TypeError for comparisons
# of different types
with pytest.raises(TypeError):
t >= val
with pytest.raises(TypeError):
t > val
with pytest.raises(TypeError):
t <= val
with pytest.raises(TypeError):
t < val


class TestTimedeltas(object):

Expand Down