Skip to content

Commit d4d21fa

Browse files
authored
ENH: make Tick comparisons match Timedelta behavior (#34088)
1 parent 9a741d3 commit d4d21fa

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ Other
866866
- Bug in :meth:`DataFrame.__dir__` caused a segfault when using unicode surrogates in a column name (:issue:`25509`)
867867
- Bug in :meth:`DataFrame.plot.scatter` caused an error when plotting variable marker sizes (:issue:`32904`)
868868
- :class:`IntegerArray` now implements the ``sum`` operation (:issue:`33172`)
869+
- Bug in :class:`Tick` comparisons raising ``TypeError`` when comparing against timedelta-like objects (:issue:`34088`)
869870

870871
.. ---------------------------------------------------------------------------
871872

pandas/tests/tseries/offsets/test_ticks.py

+19
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,22 @@ def test_compare_ticks_to_strs(cls):
320320
left > right
321321
with pytest.raises(TypeError):
322322
left >= right
323+
324+
325+
@pytest.mark.parametrize("cls", tick_classes)
326+
def test_compare_ticks_to_timedeltalike(cls):
327+
off = cls(19)
328+
329+
td = off.delta
330+
331+
others = [td, td.to_timedelta64()]
332+
if cls is not Nano:
333+
others.append(td.to_pytimedelta())
334+
335+
for other in others:
336+
assert off == other
337+
assert not off != other
338+
assert not off < other
339+
assert not off > other
340+
assert off <= other
341+
assert off >= other

pandas/tseries/offsets.py

+6-17
Original file line numberDiff line numberDiff line change
@@ -2284,17 +2284,12 @@ def is_on_offset(self, dt: datetime) -> bool:
22842284

22852285

22862286
def _tick_comp(op):
2287-
assert op not in [operator.eq, operator.ne]
2287+
"""
2288+
Tick comparisons should behave identically to Timedelta comparisons.
2289+
"""
22882290

22892291
def f(self, other):
2290-
try:
2291-
return op(self.delta, other.delta)
2292-
except AttributeError as err:
2293-
# comparing with a non-Tick object
2294-
raise TypeError(
2295-
f"Invalid comparison between {type(self).__name__} "
2296-
f"and {type(other).__name__}"
2297-
) from err
2292+
return op(self.delta, other)
22982293

22992294
f.__name__ = f"__{op.__name__}__"
23002295
return f
@@ -2346,10 +2341,7 @@ def __eq__(self, other: Any) -> bool:
23462341
# e.g. "infer"
23472342
return False
23482343

2349-
if isinstance(other, Tick):
2350-
return self.delta == other.delta
2351-
else:
2352-
return False
2344+
return _tick_comp(operator.eq)(self, other)
23532345

23542346
# This is identical to DateOffset.__hash__, but has to be redefined here
23552347
# for Python 3, because we've redefined __eq__.
@@ -2368,10 +2360,7 @@ def __ne__(self, other):
23682360
# e.g. "infer"
23692361
return True
23702362

2371-
if isinstance(other, Tick):
2372-
return self.delta != other.delta
2373-
else:
2374-
return True
2363+
return _tick_comp(operator.ne)(self, other)
23752364

23762365
@property
23772366
def delta(self) -> Timedelta:

0 commit comments

Comments
 (0)