Skip to content

Commit bf67998

Browse files
jbrockmendelPingviinituutti
authored andcommitted
* Revert pandas-dev#21394 * Typo fixup
1 parent 69aada2 commit bf67998

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

doc/source/whatsnew/v0.24.0.rst

-1
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,6 @@ Other API Changes
10821082
has an improved ``KeyError`` message, and will not fail on duplicate column names with ``drop=True``. (:issue:`22484`)
10831083
- Slicing a single row of a DataFrame with multiple ExtensionArrays of the same type now preserves the dtype, rather than coercing to object (:issue:`22784`)
10841084
- :class:`DateOffset` attribute `_cacheable` and method `_should_cache` have been removed (:issue:`23118`)
1085-
- Comparing :class:`Timedelta` to be less or greater than unknown types now raises a ``TypeError`` instead of returning ``False`` (:issue:`20829`)
10861085
- :meth:`Categorical.searchsorted`, when supplied a scalar value to search for, now returns a scalar instead of an array (:issue:`23466`).
10871086
- :meth:`Categorical.searchsorted` now raises a ``KeyError`` rather that a ``ValueError``, if a searched for key is not found in its categories (:issue:`23466`).
10881087
- :meth:`Index.hasnans` and :meth:`Series.hasnans` now always return a python boolean. Previously, a python or a numpy boolean could be returned, depending on circumstances (:issue:`23294`).

pandas/_libs/tslibs/timedeltas.pyx

+16-2
Original file line numberDiff line numberDiff line change
@@ -770,12 +770,26 @@ cdef class _Timedelta(timedelta):
770770
if is_timedelta64_object(other):
771771
other = Timedelta(other)
772772
else:
773-
return NotImplemented
773+
if op == Py_EQ:
774+
return False
775+
elif op == Py_NE:
776+
return True
777+
# only allow ==, != ops
778+
raise TypeError('Cannot compare type {cls} with '
779+
'type {other}'
780+
.format(cls=type(self).__name__,
781+
other=type(other).__name__))
774782
if util.is_array(other):
775783
return PyObject_RichCompare(np.array([self]), other, op)
776784
return PyObject_RichCompare(other, self, reverse_ops[op])
777785
else:
778-
return NotImplemented
786+
if op == Py_EQ:
787+
return False
788+
elif op == Py_NE:
789+
return True
790+
raise TypeError('Cannot compare type {cls} with type {other}'
791+
.format(cls=type(self).__name__,
792+
other=type(other).__name__))
779793

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

pandas/tests/scalar/timedelta/test_timedelta.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ def test_ops_error_str(self):
4444
with pytest.raises(TypeError):
4545
left + right
4646

47-
# GH 20829: python 2 comparison naturally does not raise TypeError
48-
if compat.PY3:
49-
with pytest.raises(TypeError):
50-
left > right
47+
with pytest.raises(TypeError):
48+
left > right
5149

5250
assert not left == right
5351
assert left != right
@@ -107,9 +105,12 @@ def test_compare_timedelta_ndarray(self):
107105
expected = np.array([False, False])
108106
tm.assert_numpy_array_equal(result, expected)
109107

108+
@pytest.mark.skip(reason="GH#20829 is reverted until after 0.24.0")
110109
def test_compare_custom_object(self):
111-
"""Make sure non supported operations on Timedelta returns NonImplemented
112-
and yields to other operand (GH20829)."""
110+
"""
111+
Make sure non supported operations on Timedelta returns NonImplemented
112+
and yields to other operand (GH#20829).
113+
"""
113114
class CustomClass(object):
114115

115116
def __init__(self, cmp_result=None):
@@ -139,11 +140,7 @@ def __gt__(self, other):
139140

140141
assert t == CustomClass(cmp_result=True)
141142

142-
@pytest.mark.skipif(compat.PY2,
143-
reason="python 2 does not raise TypeError for \
144-
comparisons of different types")
145-
@pytest.mark.parametrize("val", [
146-
"string", 1])
143+
@pytest.mark.parametrize("val", ["string", 1])
147144
def test_compare_unknown_type(self, val):
148145
# GH20829
149146
t = Timedelta('1s')

0 commit comments

Comments
 (0)