Skip to content

CLN: assorted tslibs cleanups #34432

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 1 commit into from
May 28, 2020
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
14 changes: 7 additions & 7 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ cdef class BusinessDay(BusinessMixin):
off_str += str(td.microseconds) + "us"
return off_str

if isinstance(self.offset, timedelta):
if PyDelta_Check(self.offset):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cython doesn't optimize this automatically?
(and does this make a noticeable difference?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cython does not do this automatically. it makes a small differnece, but enough that we do this in the rest of the cython code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah we use this type of check with datetime types a lot

zero = timedelta(0, 0, 0)
if self.offset >= zero:
off_str = "+" + get_str(self.offset)
Expand All @@ -1337,7 +1337,7 @@ cdef class BusinessDay(BusinessMixin):

@apply_wraps
def apply(self, other):
if isinstance(other, datetime):
if PyDateTime_Check(other):
n = self.n
wday = other.weekday()

Expand Down Expand Up @@ -1368,7 +1368,7 @@ cdef class BusinessDay(BusinessMixin):
result = result + self.offset
return result

elif isinstance(other, (timedelta, Tick)):
elif PyDelta_Check(other) or isinstance(other, Tick):
return BusinessDay(
self.n, offset=self.offset + other, normalize=self.normalize
)
Expand Down Expand Up @@ -1649,7 +1649,7 @@ cdef class BusinessHour(BusinessMixin):

@apply_wraps
def apply(self, other):
if isinstance(other, datetime):
if PyDateTime_Check(other):
# used for detecting edge condition
nanosecond = getattr(other, "nanosecond", 0)
# reset timezone and nanosecond
Expand Down Expand Up @@ -2511,7 +2511,7 @@ cdef class Week(SingleConstructorOffset):
if self.weekday is None:
return other + self.n * self._inc

if not isinstance(other, datetime):
if not PyDateTime_Check(other):
raise TypeError(
f"Cannot add {type(other).__name__} to {type(self).__name__}"
)
Expand Down Expand Up @@ -3305,7 +3305,7 @@ class CustomBusinessDay(CustomMixin, BusinessDay):
else:
roll = "backward"

if isinstance(other, datetime):
if PyDateTime_Check(other):
date_in = other
np_dt = np.datetime64(date_in.date())

Expand All @@ -3320,7 +3320,7 @@ class CustomBusinessDay(CustomMixin, BusinessDay):
result = result + self.offset
return result

elif isinstance(other, (timedelta, Tick)):
elif PyDelta_Check(other) or isinstance(other, Tick):
return BDay(self.n, offset=self.offset + other, normalize=self.normalize)
else:
raise ApplyTypeError(
Expand Down
24 changes: 6 additions & 18 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ cimport pandas._libs.tslibs.util as util
from pandas._libs.tslibs.timestamps import Timestamp
from pandas._libs.tslibs.timezones cimport is_utc, is_tzlocal, get_dst_info
from pandas._libs.tslibs.timedeltas import Timedelta
from pandas._libs.tslibs.timedeltas cimport delta_to_nanoseconds
from pandas._libs.tslibs.timedeltas cimport (
delta_to_nanoseconds,
is_any_td_scalar,
)

from pandas._libs.tslibs.ccalendar cimport (
dayofweek,
Expand Down Expand Up @@ -1591,7 +1594,7 @@ cdef class _Period:
return NaT
return other.__add__(self)

if is_any_tdlike_scalar(other):
if is_any_td_scalar(other):
return self._add_delta(other)
elif is_offset_object(other):
return self._add_offset(other)
Expand All @@ -1618,7 +1621,7 @@ cdef class _Period:
return NaT
return NotImplemented

elif is_any_tdlike_scalar(other):
elif is_any_td_scalar(other):
neg_other = -other
return self + neg_other
elif is_offset_object(other):
Expand Down Expand Up @@ -2494,18 +2497,3 @@ def validate_end_alias(how):
if how not in {'S', 'E'}:
raise ValueError('How must be one of S or E')
return how


cpdef is_any_tdlike_scalar(object obj):
"""
Cython equivalent for `isinstance(obj, (timedelta, np.timedelta64, Tick))`

Parameters
----------
obj : object

Returns
-------
bool
"""
return util.is_timedelta64_object(obj) or PyDelta_Check(obj) or is_tick_object(obj)
1 change: 1 addition & 0 deletions pandas/_libs/tslibs/timedeltas.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ from numpy cimport int64_t
# Exposed for tslib, not intended for outside use.
cpdef int64_t delta_to_nanoseconds(delta) except? -1
cdef convert_to_timedelta64(object ts, object unit)
cdef bint is_any_td_scalar(object obj)
14 changes: 13 additions & 1 deletion pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ cdef inline timedelta_from_spec(object number, object frac, object unit):
frac : a list of frac digits
unit : a list of unit characters
"""
cdef object n
cdef:
str n

try:
unit = ''.join(unit)
Expand Down Expand Up @@ -1376,6 +1377,17 @@ class Timedelta(_Timedelta):


cdef bint is_any_td_scalar(object obj):
"""
Cython equivalent for `isinstance(obj, (timedelta, np.timedelta64, Tick))`

Parameters
----------
obj : object

Returns
-------
bool
"""
return (
PyDelta_Check(obj) or is_timedelta64_object(obj) or is_tick_object(obj)
)
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ class Timestamp(_Timestamp):
# check that only ts_input is passed
# checking verbosely, because cython doesn't optimize
# list comprehensions (as of cython 0.29.x)
if (isinstance(ts_input, Timestamp) and freq is None and
if (isinstance(ts_input, _Timestamp) and freq is None and
tz is None and unit is None and year is None and
month is None and day is None and hour is None and
minute is None and second is None and
Expand Down