Skip to content

Commit 41bb434

Browse files
authored
CLN: remove cython<3 reversed-op pattern (#56006)
1 parent c9c58ae commit 41bb434

File tree

5 files changed

+5
-84
lines changed

5 files changed

+5
-84
lines changed

pandas/_libs/interval.pyx

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -495,17 +495,6 @@ cdef class Interval(IntervalMixin):
495495
or cnp.is_timedelta64_object(y)
496496
):
497497
return Interval(self.left + y, self.right + y, closed=self.closed)
498-
elif (
499-
# __radd__ pattern
500-
# TODO(cython3): remove this
501-
isinstance(y, Interval)
502-
and (
503-
isinstance(self, numbers.Number)
504-
or PyDelta_Check(self)
505-
or cnp.is_timedelta64_object(self)
506-
)
507-
):
508-
return Interval(y.left + self, y.right + self, closed=y.closed)
509498
return NotImplemented
510499

511500
def __radd__(self, other):
@@ -529,10 +518,6 @@ cdef class Interval(IntervalMixin):
529518
def __mul__(self, y):
530519
if isinstance(y, numbers.Number):
531520
return Interval(self.left * y, self.right * y, closed=self.closed)
532-
elif isinstance(y, Interval) and isinstance(self, numbers.Number):
533-
# __radd__ semantics
534-
# TODO(cython3): remove this
535-
return Interval(y.left * self, y.right * self, closed=y.closed)
536521
return NotImplemented
537522

538523
def __rmul__(self, other):

pandas/_libs/tslibs/nattype.pyx

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@ cdef class _NaT(datetime):
124124
return NotImplemented
125125

126126
def __add__(self, other):
127-
if self is not c_NaT:
128-
# TODO(cython3): remove this it moved to __radd__
129-
# cython __radd__ semantics
130-
self, other = other, self
131-
132127
if PyDateTime_Check(other):
133128
return c_NaT
134129
elif PyDelta_Check(other):
@@ -158,14 +153,6 @@ cdef class _NaT(datetime):
158153
def __sub__(self, other):
159154
# Duplicate some logic from _Timestamp.__sub__ to avoid needing
160155
# to subclass; allows us to @final(_Timestamp.__sub__)
161-
cdef:
162-
bint is_rsub = False
163-
164-
if self is not c_NaT:
165-
# cython __rsub__ semantics
166-
# TODO(cython3): remove __rsub__ logic from here
167-
self, other = other, self
168-
is_rsub = True
169156

170157
if PyDateTime_Check(other):
171158
return c_NaT
@@ -180,19 +167,9 @@ cdef class _NaT(datetime):
180167

181168
elif util.is_array(other):
182169
if other.dtype.kind == "m":
183-
if not is_rsub:
184-
# NaT - timedelta64 we treat NaT as datetime64, so result
185-
# is datetime64
186-
result = np.empty(other.shape, dtype="datetime64[ns]")
187-
result.fill("NaT")
188-
return result
189-
190-
# __rsub__ logic here
191-
# TODO(cython3): remove this, move above code out of
192-
# ``if not is_rsub`` block
193-
# timedelta64 - NaT we have to treat NaT as timedelta64
194-
# for this to be meaningful, and the result is timedelta64
195-
result = np.empty(other.shape, dtype="timedelta64[ns]")
170+
# NaT - timedelta64 we treat NaT as datetime64, so result
171+
# is datetime64
172+
result = np.empty(other.shape, dtype="datetime64[ns]")
196173
result.fill("NaT")
197174
return result
198175

pandas/_libs/tslibs/offsets.pyx

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -489,12 +489,7 @@ cdef class BaseOffset:
489489
return type(self)(n=1, normalize=self.normalize, **self.kwds)
490490

491491
def __add__(self, other):
492-
if not isinstance(self, BaseOffset):
493-
# cython semantics; this is __radd__
494-
# TODO(cython3): remove this, this moved to __radd__
495-
return other.__add__(self)
496-
497-
elif util.is_array(other) and other.dtype == object:
492+
if util.is_array(other) and other.dtype == object:
498493
return np.array([self + x for x in other])
499494

500495
try:
@@ -511,10 +506,6 @@ cdef class BaseOffset:
511506
elif type(other) is type(self):
512507
return type(self)(self.n - other.n, normalize=self.normalize,
513508
**self.kwds)
514-
elif not isinstance(self, BaseOffset):
515-
# TODO(cython3): remove, this moved to __rsub__
516-
# cython semantics, this is __rsub__
517-
return (-other).__add__(self)
518509
else:
519510
# e.g. PeriodIndex
520511
return NotImplemented
@@ -528,10 +519,6 @@ cdef class BaseOffset:
528519
elif is_integer_object(other):
529520
return type(self)(n=other * self.n, normalize=self.normalize,
530521
**self.kwds)
531-
elif not isinstance(self, BaseOffset):
532-
# TODO(cython3): remove this, this moved to __rmul__
533-
# cython semantics, this is __rmul__
534-
return other.__mul__(self)
535522
return NotImplemented
536523

537524
def __rmul__(self, other):
@@ -1025,10 +1012,6 @@ cdef class Tick(SingleConstructorOffset):
10251012
return self.delta.__gt__(other)
10261013

10271014
def __mul__(self, other):
1028-
if not isinstance(self, Tick):
1029-
# TODO(cython3), remove this, this moved to __rmul__
1030-
# cython semantics, this is __rmul__
1031-
return other.__mul__(self)
10321015
if is_float_object(other):
10331016
n = other * self.n
10341017
# If the new `n` is an integer, we can represent it using the
@@ -1056,11 +1039,6 @@ cdef class Tick(SingleConstructorOffset):
10561039
return _wrap_timedelta_result(result)
10571040

10581041
def __add__(self, other):
1059-
if not isinstance(self, Tick):
1060-
# cython semantics; this is __radd__
1061-
# TODO(cython3): remove this, this moved to __radd__
1062-
return other.__add__(self)
1063-
10641042
if isinstance(other, Tick):
10651043
if type(self) is type(other):
10661044
return type(self)(self.n + other.n)

pandas/_libs/tslibs/period.pyx

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,13 +1855,6 @@ cdef class _Period(PeriodMixin):
18551855

18561856
@cython.overflowcheck(True)
18571857
def __add__(self, other):
1858-
if not is_period_object(self):
1859-
# cython semantics; this is analogous to a call to __radd__
1860-
# TODO(cython3): remove this
1861-
if self is NaT:
1862-
return NaT
1863-
return other.__add__(self)
1864-
18651858
if is_any_td_scalar(other):
18661859
return self._add_timedeltalike_scalar(other)
18671860
elif is_offset_object(other):
@@ -1893,14 +1886,7 @@ cdef class _Period(PeriodMixin):
18931886
return self.__add__(other)
18941887

18951888
def __sub__(self, other):
1896-
if not is_period_object(self):
1897-
# cython semantics; this is like a call to __rsub__
1898-
# TODO(cython3): remove this
1899-
if self is NaT:
1900-
return NaT
1901-
return NotImplemented
1902-
1903-
elif (
1889+
if (
19041890
is_any_td_scalar(other)
19051891
or is_offset_object(other)
19061892
or util.is_integer_object(other)

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,6 @@ cdef class _Timestamp(ABCTimestamp):
475475
dtype=object,
476476
)
477477

478-
elif not isinstance(self, _Timestamp):
479-
# cython semantics, args have been switched and this is __radd__
480-
# TODO(cython3): remove this it moved to __radd__
481-
return other.__add__(self)
482-
483478
return NotImplemented
484479

485480
def __radd__(self, other):

0 commit comments

Comments
 (0)