Skip to content

implement Tick division, fix Timedelta.__cmp__ tick #24710

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 6 commits into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from all 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.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,7 @@ Timedelta
- Bug in :class:`Timedelta` and :func:`to_timedelta()` have inconsistencies in supported unit string (:issue:`21762`)
- Bug in :class:`TimedeltaIndex` division where dividing by another :class:`TimedeltaIndex` raised ``TypeError`` instead of returning a :class:`Float64Index` (:issue:`23829`, :issue:`22631`)
- Bug in :class:`TimedeltaIndex` comparison operations where comparing against non-``Timedelta``-like objects would raise ``TypeError`` instead of returning all-``False`` for ``__eq__`` and all-``True`` for ``__ne__`` (:issue:`24056`)
- Bug in :class:`Timedelta` comparisons when comparing with a ``Tick`` object incorrectly raising ``TypeError`` (:issue:`24710`)

Timezones
^^^^^^^^^
Expand Down
32 changes: 31 additions & 1 deletion pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cython
import time
from cpython.datetime cimport (PyDateTime_IMPORT,
PyDateTime_Check,
PyDelta_Check,
datetime, timedelta,
time as dt_time)
PyDateTime_IMPORT
Expand All @@ -28,6 +29,9 @@ from pandas._libs.tslibs.np_datetime cimport (
npy_datetimestruct, dtstruct_to_dt64, dt64_to_dtstruct)
from pandas._libs.tslibs.timezones import UTC


PY2 = bytes == str

# ---------------------------------------------------------------------
# Constants

Expand Down Expand Up @@ -126,6 +130,26 @@ def apply_index_wraps(func):
return wrapper


cdef _wrap_timedelta_result(result):
"""
Tick operations dispatch to their Timedelta counterparts. Wrap the result
of these operations in a Tick if possible.

Parameters
----------
result : object

Returns
-------
object
"""
if PyDelta_Check(result):
# convert Timedelta back to a Tick
from pandas.tseries.offsets import _delta_to_tick
return _delta_to_tick(result)

return result

# ---------------------------------------------------------------------
# Business Helpers

Expand Down Expand Up @@ -508,7 +532,13 @@ class _Tick(object):
dummy class to mix into tseries.offsets.Tick so that in tslibs.period we
can do isinstance checks on _Tick and avoid importing tseries.offsets
"""
pass

def __truediv__(self, other):
result = self.delta.__truediv__(other)
return _wrap_timedelta_result(result)

if PY2:
__div__ = __truediv__


# ----------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ from pandas._libs.tslibs.nattype import nat_strings
from pandas._libs.tslibs.nattype cimport (
checknull_with_nat, NPY_NAT, c_NaT as NaT)
from pandas._libs.tslibs.offsets cimport to_offset
from pandas._libs.tslibs.offsets import _Tick as Tick

# ----------------------------------------------------------------------
# Constants
Expand Down Expand Up @@ -757,7 +758,7 @@ cdef class _Timedelta(timedelta):

if isinstance(other, _Timedelta):
ots = other
elif PyDelta_Check(other):
elif PyDelta_Check(other) or isinstance(other, Tick):
ots = Timedelta(other)
else:
ndim = getattr(other, "ndim", -1)
Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ def test_numeric_arr_mul_tdscalar(self, scalar_td, numeric_idx, box):
tm.assert_equal(commute, expected)

def test_numeric_arr_rdiv_tdscalar(self, three_days, numeric_idx, box):

if box is not pd.Index and isinstance(three_days, pd.offsets.Tick):
raise pytest.xfail("Tick division not implemented")

index = numeric_idx[1:3]

expected = TimedeltaIndex(['3 Days', '36 Hours'])
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ def test_unary_ops(self):


class TestTimedeltaComparison(object):
def test_compare_tick(self, tick_classes):
cls = tick_classes

off = cls(4)
td = off.delta
assert isinstance(td, Timedelta)

assert td == off
assert not td != off
assert td <= off
assert td >= off
assert not td < off
assert not td > off

assert not td == 2 * off
assert td != 2 * off
assert td <= 2 * off
assert td < 2 * off
assert not td >= 2 * off
assert not td > 2 * off

def test_comparison_object_array(self):
# analogous to GH#15183
td = Timedelta('2 days')
Expand Down
34 changes: 34 additions & 0 deletions pandas/tests/tseries/offsets/test_ticks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"""
Tests for offsets.Tick and subclasses
"""
from __future__ import division

from datetime import datetime, timedelta

from hypothesis import assume, example, given, settings, strategies as st
Expand Down Expand Up @@ -36,6 +38,10 @@ def test_delta_to_tick():
tick = offsets._delta_to_tick(delta)
assert (tick == offsets.Day(3))

td = Timedelta(nanoseconds=5)
tick = offsets._delta_to_tick(td)
assert tick == Nano(5)


@pytest.mark.parametrize('cls', tick_classes)
@settings(deadline=None) # GH 24641
Expand Down Expand Up @@ -228,6 +234,34 @@ def test_tick_addition(kls, expected):
assert result == expected


@pytest.mark.parametrize('cls', tick_classes)
def test_tick_division(cls):
off = cls(10)

assert off / cls(5) == 2
assert off / 2 == cls(5)
assert off / 2.0 == cls(5)

assert off / off.delta == 1
assert off / off.delta.to_timedelta64() == 1

assert off / Nano(1) == off.delta / Nano(1).delta

if cls is not Nano:
# A case where we end up with a smaller class
result = off / 1000
assert isinstance(result, offsets.Tick)
assert not isinstance(result, cls)
assert result.delta == off.delta / 1000

if cls._inc < Timedelta(seconds=1):
# Case where we end up with a bigger class
result = off / .001
assert isinstance(result, offsets.Tick)
assert not isinstance(result, cls)
assert result.delta == off.delta / .001


@pytest.mark.parametrize('cls1', tick_classes)
@pytest.mark.parametrize('cls2', tick_classes)
def test_tick_zero(cls1, cls2):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2343,7 +2343,8 @@ def isAnchored(self):


def _delta_to_tick(delta):
if delta.microseconds == 0:
if delta.microseconds == 0 and getattr(delta, "nanoseconds", 0) == 0:
# nanoseconds only for pd.Timedelta
if delta.seconds == 0:
return Day(delta.days)
else:
Expand Down