Skip to content

ENH: make Tick comparisons match Timedelta behavior #34088

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 10 commits into from
May 12, 2020
Merged
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ Other
- Bug in :meth:`DataFrame.__dir__` caused a segfault when using unicode surrogates in a column name (:issue:`25509`)
- Bug in :meth:`DataFrame.plot.scatter` caused an error when plotting variable marker sizes (:issue:`32904`)
- :class:`IntegerArray` now implements the ``sum`` operation (:issue:`33172`)
- Bug in :class:`Tick` comparisons raising ``TypeError`` when comparing against timedelta-like objects (:issue:`34088`)

.. ---------------------------------------------------------------------------

Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/tseries/offsets/test_ticks.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,22 @@ def test_compare_ticks_to_strs(cls):
left > right
with pytest.raises(TypeError):
left >= right


@pytest.mark.parametrize("cls", tick_classes)
def test_compare_ticks_to_timedeltalike(cls):
off = cls(19)

td = off.delta

others = [td, td.to_timedelta64()]
if cls is not Nano:
others.append(td.to_pytimedelta())

for other in others:
assert off == other
assert not off != other
assert not off < other
assert not off > other
assert off <= other
assert off >= other
23 changes: 6 additions & 17 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2284,17 +2284,12 @@ def is_on_offset(self, dt: datetime) -> bool:


def _tick_comp(op):
assert op not in [operator.eq, operator.ne]
"""
Tick comparisons should behave identically to Timedelta comparisons.
"""

def f(self, other):
try:
return op(self.delta, other.delta)
except AttributeError as err:
# comparing with a non-Tick object
raise TypeError(
f"Invalid comparison between {type(self).__name__} "
f"and {type(other).__name__}"
) from err
return op(self.delta, other)

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

if isinstance(other, Tick):
return self.delta == other.delta
else:
return False
return _tick_comp(operator.eq)(self, other)

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

if isinstance(other, Tick):
return self.delta != other.delta
else:
return True
return _tick_comp(operator.ne)(self, other)

@property
def delta(self) -> Timedelta:
Expand Down