Skip to content

Commit f347410

Browse files
jbrockmendelvictor
authored and
victor
committed
move _add__ and __sub__ to liboffsets, implement _Tick (pandas-dev#21694)
1 parent a5fa6bd commit f347410

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

pandas/_libs/tslibs/offsets.pyx

+29-2
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,31 @@ class _BaseOffset(object):
368368
if name not in ['n', 'normalize']}
369369
return {name: kwds[name] for name in kwds if kwds[name] is not None}
370370

371+
def __add__(self, other):
372+
if getattr(other, "_typ", None) in ["datetimeindex",
373+
"series", "period"]:
374+
# defer to the other class's implementation
375+
return other + self
376+
try:
377+
return self.apply(other)
378+
except ApplyTypeError:
379+
return NotImplemented
380+
381+
def __sub__(self, other):
382+
if isinstance(other, datetime):
383+
raise TypeError('Cannot subtract datetime from offset.')
384+
elif type(other) == type(self):
385+
return type(self)(self.n - other.n, normalize=self.normalize,
386+
**self.kwds)
387+
else: # pragma: no cover
388+
return NotImplemented
389+
371390
def __call__(self, other):
372391
return self.apply(other)
373392

374393
def __mul__(self, other):
375-
return self.__class__(n=other * self.n, normalize=self.normalize,
376-
**self.kwds)
394+
return type(self)(n=other * self.n, normalize=self.normalize,
395+
**self.kwds)
377396

378397
def __neg__(self):
379398
# Note: we are defering directly to __mul__ instead of __rmul__, as
@@ -495,6 +514,14 @@ class BaseOffset(_BaseOffset):
495514
return -self + other
496515

497516

517+
class _Tick(object):
518+
"""
519+
dummy class to mix into tseries.offsets.Tick so that in tslibs.period we
520+
can do isinstance checks on _Tick and avoid importing tseries.offsets
521+
"""
522+
pass
523+
524+
498525
# ----------------------------------------------------------------------
499526
# RelativeDelta Arithmetic
500527

pandas/_libs/tslibs/period.pyx

+3-4
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ from resolution import Resolution
5252
from nattype import nat_strings, NaT, iNaT
5353
from nattype cimport _nat_scalar_rules, NPY_NAT, is_null_datetimelike
5454
from offsets cimport to_offset
55-
56-
from pandas.tseries import offsets
55+
from offsets import _Tick
5756

5857
cdef bint PY2 = str == bytes
5958

@@ -1060,9 +1059,9 @@ cdef class _Period(object):
10601059
int64_t nanos, offset_nanos
10611060

10621061
if (PyDelta_Check(other) or util.is_timedelta64_object(other) or
1063-
isinstance(other, offsets.Tick)):
1062+
isinstance(other, _Tick)):
10641063
offset = to_offset(self.freq.rule_code)
1065-
if isinstance(offset, offsets.Tick):
1064+
if isinstance(offset, _Tick):
10661065
nanos = delta_to_nanoseconds(other)
10671066
offset_nanos = delta_to_nanoseconds(offset)
10681067
if nanos % offset_nanos == 0:

pandas/tseries/offsets.py

+2-21
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pandas import compat
88
import numpy as np
99

10-
from pandas.core.dtypes.generic import ABCSeries, ABCDatetimeIndex, ABCPeriod
10+
from pandas.core.dtypes.generic import ABCPeriod
1111
from pandas.core.tools.datetimes import to_datetime
1212
import pandas.core.common as com
1313

@@ -288,25 +288,6 @@ def _repr_attrs(self):
288288
def name(self):
289289
return self.rule_code
290290

291-
def __add__(self, other):
292-
if isinstance(other, (ABCDatetimeIndex, ABCSeries)):
293-
return other + self
294-
elif isinstance(other, ABCPeriod):
295-
return other + self
296-
try:
297-
return self.apply(other)
298-
except ApplyTypeError:
299-
return NotImplemented
300-
301-
def __sub__(self, other):
302-
if isinstance(other, datetime):
303-
raise TypeError('Cannot subtract datetime from offset.')
304-
elif type(other) == type(self):
305-
return self.__class__(self.n - other.n, normalize=self.normalize,
306-
**self.kwds)
307-
else: # pragma: no cover
308-
return NotImplemented
309-
310291
def rollback(self, dt):
311292
"""Roll provided date backward to next offset only if not on offset"""
312293
dt = as_timestamp(dt)
@@ -2106,7 +2087,7 @@ def f(self, other):
21062087
return f
21072088

21082089

2109-
class Tick(SingleConstructorOffset):
2090+
class Tick(liboffsets._Tick, SingleConstructorOffset):
21102091
_inc = Timedelta(microseconds=1000)
21112092
_prefix = 'undefined'
21122093
_attributes = frozenset(['n', 'normalize'])

0 commit comments

Comments
 (0)