Skip to content

Commit 9223d19

Browse files
authored
CLN: assorted tslibs cleanups (#34432)
1 parent 63dda53 commit 9223d19

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

pandas/_libs/tslibs/offsets.pyx

+7-7
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ cdef class BusinessDay(BusinessMixin):
13431343
off_str += str(td.microseconds) + "us"
13441344
return off_str
13451345

1346-
if isinstance(self.offset, timedelta):
1346+
if PyDelta_Check(self.offset):
13471347
zero = timedelta(0, 0, 0)
13481348
if self.offset >= zero:
13491349
off_str = "+" + get_str(self.offset)
@@ -1355,7 +1355,7 @@ cdef class BusinessDay(BusinessMixin):
13551355

13561356
@apply_wraps
13571357
def apply(self, other):
1358-
if isinstance(other, datetime):
1358+
if PyDateTime_Check(other):
13591359
n = self.n
13601360
wday = other.weekday()
13611361

@@ -1386,7 +1386,7 @@ cdef class BusinessDay(BusinessMixin):
13861386
result = result + self.offset
13871387
return result
13881388

1389-
elif isinstance(other, (timedelta, Tick)):
1389+
elif PyDelta_Check(other) or isinstance(other, Tick):
13901390
return BusinessDay(
13911391
self.n, offset=self.offset + other, normalize=self.normalize
13921392
)
@@ -1667,7 +1667,7 @@ cdef class BusinessHour(BusinessMixin):
16671667

16681668
@apply_wraps
16691669
def apply(self, other):
1670-
if isinstance(other, datetime):
1670+
if PyDateTime_Check(other):
16711671
# used for detecting edge condition
16721672
nanosecond = getattr(other, "nanosecond", 0)
16731673
# reset timezone and nanosecond
@@ -2510,7 +2510,7 @@ cdef class Week(SingleConstructorOffset):
25102510
if self.weekday is None:
25112511
return other + self.n * self._inc
25122512

2513-
if not isinstance(other, datetime):
2513+
if not PyDateTime_Check(other):
25142514
raise TypeError(
25152515
f"Cannot add {type(other).__name__} to {type(self).__name__}"
25162516
)
@@ -3304,7 +3304,7 @@ cdef class CustomBusinessDay(BusinessDay):
33043304
else:
33053305
roll = "backward"
33063306

3307-
if isinstance(other, datetime):
3307+
if PyDateTime_Check(other):
33083308
date_in = other
33093309
np_dt = np.datetime64(date_in.date())
33103310

@@ -3319,7 +3319,7 @@ cdef class CustomBusinessDay(BusinessDay):
33193319
result = result + self.offset
33203320
return result
33213321

3322-
elif isinstance(other, (timedelta, Tick)):
3322+
elif PyDelta_Check(other) or isinstance(other, Tick):
33233323
return BDay(self.n, offset=self.offset + other, normalize=self.normalize)
33243324
else:
33253325
raise ApplyTypeError(

pandas/_libs/tslibs/period.pyx

+6-18
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ cimport pandas._libs.tslibs.util as util
4040
from pandas._libs.tslibs.timestamps import Timestamp
4141
from pandas._libs.tslibs.timezones cimport is_utc, is_tzlocal, get_dst_info
4242
from pandas._libs.tslibs.timedeltas import Timedelta
43-
from pandas._libs.tslibs.timedeltas cimport delta_to_nanoseconds
43+
from pandas._libs.tslibs.timedeltas cimport (
44+
delta_to_nanoseconds,
45+
is_any_td_scalar,
46+
)
4447

4548
from pandas._libs.tslibs.ccalendar cimport (
4649
dayofweek,
@@ -1591,7 +1594,7 @@ cdef class _Period:
15911594
return NaT
15921595
return other.__add__(self)
15931596

1594-
if is_any_tdlike_scalar(other):
1597+
if is_any_td_scalar(other):
15951598
return self._add_delta(other)
15961599
elif is_offset_object(other):
15971600
return self._add_offset(other)
@@ -1618,7 +1621,7 @@ cdef class _Period:
16181621
return NaT
16191622
return NotImplemented
16201623

1621-
elif is_any_tdlike_scalar(other):
1624+
elif is_any_td_scalar(other):
16221625
neg_other = -other
16231626
return self + neg_other
16241627
elif is_offset_object(other):
@@ -2494,18 +2497,3 @@ def validate_end_alias(how):
24942497
if how not in {'S', 'E'}:
24952498
raise ValueError('How must be one of S or E')
24962499
return how
2497-
2498-
2499-
cpdef is_any_tdlike_scalar(object obj):
2500-
"""
2501-
Cython equivalent for `isinstance(obj, (timedelta, np.timedelta64, Tick))`
2502-
2503-
Parameters
2504-
----------
2505-
obj : object
2506-
2507-
Returns
2508-
-------
2509-
bool
2510-
"""
2511-
return util.is_timedelta64_object(obj) or PyDelta_Check(obj) or is_tick_object(obj)

pandas/_libs/tslibs/timedeltas.pxd

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ from numpy cimport int64_t
33
# Exposed for tslib, not intended for outside use.
44
cpdef int64_t delta_to_nanoseconds(delta) except? -1
55
cdef convert_to_timedelta64(object ts, object unit)
6+
cdef bint is_any_td_scalar(object obj)

pandas/_libs/tslibs/timedeltas.pyx

+13-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ cdef inline timedelta_from_spec(object number, object frac, object unit):
446446
frac : a list of frac digits
447447
unit : a list of unit characters
448448
"""
449-
cdef object n
449+
cdef:
450+
str n
450451

451452
try:
452453
unit = ''.join(unit)
@@ -1376,6 +1377,17 @@ class Timedelta(_Timedelta):
13761377

13771378

13781379
cdef bint is_any_td_scalar(object obj):
1380+
"""
1381+
Cython equivalent for `isinstance(obj, (timedelta, np.timedelta64, Tick))`
1382+
1383+
Parameters
1384+
----------
1385+
obj : object
1386+
1387+
Returns
1388+
-------
1389+
bool
1390+
"""
13791391
return (
13801392
PyDelta_Check(obj) or is_timedelta64_object(obj) or is_tick_object(obj)
13811393
)

pandas/_libs/tslibs/timestamps.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ class Timestamp(_Timestamp):
808808
# check that only ts_input is passed
809809
# checking verbosely, because cython doesn't optimize
810810
# list comprehensions (as of cython 0.29.x)
811-
if (isinstance(ts_input, Timestamp) and freq is None and
811+
if (isinstance(ts_input, _Timestamp) and freq is None and
812812
tz is None and unit is None and year is None and
813813
month is None and day is None and hour is None and
814814
minute is None and second is None and

0 commit comments

Comments
 (0)