Skip to content

Commit 1291259

Browse files
jbrockmendelrhshadrach
authored andcommitted
CLN: avoid _typ checks in _libs (pandas-dev#34084)
1 parent 63aeaf9 commit 1291259

File tree

3 files changed

+6
-24
lines changed

3 files changed

+6
-24
lines changed

pandas/_libs/tslibs/offsets.pyx

+2-10
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def apply_index_wraps(func):
127127
# not play nicely with cython class methods
128128
def wrapper(self, other):
129129

130-
is_index = getattr(other, "_typ", "") == "datetimeindex"
130+
is_index = not util.is_array(other._data)
131131

132132
# operate on DatetimeArray
133133
arr = other._data if is_index else other
@@ -412,11 +412,6 @@ class _BaseOffset:
412412
return type(self)(n=1, normalize=self.normalize, **self.kwds)
413413

414414
def __add__(self, other):
415-
if getattr(other, "_typ", None) in ["datetimeindex", "periodindex",
416-
"datetimearray", "periodarray",
417-
"series", "period", "dataframe"]:
418-
# defer to the other class's implementation
419-
return other + self
420415
try:
421416
return self.apply(other)
422417
except ApplyTypeError:
@@ -613,10 +608,7 @@ class BaseOffset(_BaseOffset):
613608
return self.__add__(other)
614609

615610
def __rsub__(self, other):
616-
if getattr(other, '_typ', None) in ['datetimeindex', 'series']:
617-
# i.e. isinstance(other, (ABCDatetimeIndex, ABCSeries))
618-
return other - self
619-
return -self + other
611+
return (-self).__add__(other)
620612

621613

622614
cdef class _Tick(ABCTick):

pandas/_libs/tslibs/timedeltas.pyx

+2-13
Original file line numberDiff line numberDiff line change
@@ -579,21 +579,9 @@ def _binary_op_method_timedeltalike(op, name):
579579
# define a binary operation that only works if the other argument is
580580
# timedelta like or an array of timedeltalike
581581
def f(self, other):
582-
if hasattr(other, '_typ'):
583-
# Series, DataFrame, ...
584-
if other._typ == 'dateoffset' and hasattr(other, 'delta'):
585-
# Tick offset
586-
return op(self, other.delta)
587-
return NotImplemented
588-
589-
elif other is NaT:
582+
if other is NaT:
590583
return NaT
591584

592-
elif is_timedelta64_object(other):
593-
# convert to Timedelta below; avoid catching this in
594-
# has-dtype check before then
595-
pass
596-
597585
elif is_datetime64_object(other) or (
598586
PyDateTime_Check(other) and not isinstance(other, ABCTimestamp)):
599587
# this case is for a datetime object that is specifically
@@ -614,6 +602,7 @@ def _binary_op_method_timedeltalike(op, name):
614602
return NotImplemented
615603

616604
elif not _validate_ops_compat(other):
605+
# Includes any of our non-cython classes
617606
return NotImplemented
618607

619608
try:

pandas/tseries/offsets.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def wrapper(self, other):
8585
elif isinstance(other, (np.datetime64, datetime, date)):
8686
other = Timestamp(other)
8787
else:
88-
raise TypeError(other)
88+
# This will end up returning NotImplemented back in __add__
89+
raise ApplyTypeError
8990

9091
tz = other.tzinfo
9192
nano = other.nanosecond

0 commit comments

Comments
 (0)