Skip to content

Commit 740f94f

Browse files
authored
REF: avoid runtime imports in offsets (#34563)
1 parent 34edd52 commit 740f94f

File tree

2 files changed

+7
-29
lines changed

2 files changed

+7
-29
lines changed

pandas/_libs/tslibs/offsets.pyx

+6-28
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ from pandas._libs.tslibs.util cimport (
3131
is_float_object,
3232
)
3333

34-
from pandas._libs.tslibs.base cimport ABCTimestamp
35-
3634
from pandas._libs.tslibs.ccalendar import (
3735
MONTH_ALIASES, MONTH_TO_CAL_NUM, weekday_to_int, int_to_weekday,
3836
)
@@ -50,7 +48,9 @@ from pandas._libs.tslibs.tzconversion cimport tz_convert_single
5048

5149
from .dtypes cimport PeriodDtypeCode
5250
from .timedeltas cimport delta_to_nanoseconds
53-
51+
from .timedeltas import Timedelta
52+
from .timestamps cimport _Timestamp
53+
from .timestamps import Timestamp
5454

5555
# ---------------------------------------------------------------------
5656
# Misc Helpers
@@ -64,7 +64,7 @@ cdef bint is_tick_object(object obj):
6464

6565

6666
cdef datetime _as_datetime(datetime obj):
67-
if isinstance(obj, ABCTimestamp):
67+
if isinstance(obj, _Timestamp):
6868
return obj.to_pydatetime()
6969
return obj
7070

@@ -73,7 +73,7 @@ cdef bint _is_normalized(datetime dt):
7373
if dt.hour != 0 or dt.minute != 0 or dt.second != 0 or dt.microsecond != 0:
7474
# Regardless of whether dt is datetime vs Timestamp
7575
return False
76-
if isinstance(dt, ABCTimestamp):
76+
if isinstance(dt, _Timestamp):
7777
return dt.nanosecond == 0
7878
return True
7979

@@ -108,7 +108,6 @@ def apply_wraps(func):
108108
# not play nicely with cython class methods
109109

110110
def wrapper(self, other):
111-
from pandas import Timestamp
112111

113112
if other is NaT:
114113
return NaT
@@ -585,7 +584,6 @@ cdef class BaseOffset:
585584
TimeStamp
586585
Rolled timestamp if not on offset, otherwise unchanged timestamp.
587586
"""
588-
from pandas import Timestamp
589587
dt = Timestamp(dt)
590588
if not self.is_on_offset(dt):
591589
dt = dt - type(self)(1, normalize=self.normalize, **self.kwds)
@@ -600,7 +598,6 @@ cdef class BaseOffset:
600598
TimeStamp
601599
Rolled timestamp if not on offset, otherwise unchanged timestamp.
602600
"""
603-
from pandas import Timestamp
604601
dt = Timestamp(dt)
605602
if not self.is_on_offset(dt):
606603
dt = dt + type(self)(1, normalize=self.normalize, **self.kwds)
@@ -767,7 +764,6 @@ cdef class Tick(SingleConstructorOffset):
767764

768765
@property
769766
def delta(self):
770-
from .timedeltas import Timedelta
771767
return self.n * Timedelta(self._nanos_inc)
772768

773769
@property
@@ -854,7 +850,7 @@ cdef class Tick(SingleConstructorOffset):
854850

855851
def apply(self, other):
856852
# Timestamp can handle tz and nano sec, thus no need to use apply_wraps
857-
if isinstance(other, ABCTimestamp):
853+
if isinstance(other, _Timestamp):
858854

859855
# GH#15126
860856
# in order to avoid a recursive
@@ -869,7 +865,6 @@ cdef class Tick(SingleConstructorOffset):
869865
return NaT
870866
elif is_datetime64_object(other) or PyDate_Check(other):
871867
# PyDate_Check includes date, datetime
872-
from pandas import Timestamp
873868
return Timestamp(other) + self
874869

875870
if PyDelta_Check(other):
@@ -1028,7 +1023,6 @@ cdef class RelativeDeltaOffset(BaseOffset):
10281023
# bring tz back from UTC calculation
10291024
other = localize_pydatetime(other, tzinfo)
10301025

1031-
from .timestamps import Timestamp
10321026
return Timestamp(other)
10331027
else:
10341028
return other + timedelta(self.n)
@@ -1077,7 +1071,6 @@ cdef class RelativeDeltaOffset(BaseOffset):
10771071
if k in ["days", "hours", "minutes", "seconds", "microseconds"]
10781072
}
10791073
if timedelta_kwds:
1080-
from .timedeltas import Timedelta
10811074
delta = Timedelta(**timedelta_kwds)
10821075
index = index + (self.n * delta)
10831076
return index
@@ -2291,7 +2284,6 @@ cdef class SemiMonthOffset(SingleConstructorOffset):
22912284
@apply_index_wraps
22922285
def apply_index(self, dtindex):
22932286
# determine how many days away from the 1st of the month we are
2294-
from pandas import Timedelta
22952287

22962288
dti = dtindex
22972289
i8other = dtindex.asi8
@@ -2394,8 +2386,6 @@ cdef class SemiMonthEnd(SemiMonthOffset):
23942386
-------
23952387
result : DatetimeIndex
23962388
"""
2397-
from pandas import Timedelta
2398-
23992389
nanos = (roll % 2) * Timedelta(days=self.day_of_month).value
24002390
dtindex += nanos.astype("timedelta64[ns]")
24012391
return dtindex + Timedelta(days=-1)
@@ -2453,7 +2443,6 @@ cdef class SemiMonthBegin(SemiMonthOffset):
24532443
-------
24542444
result : DatetimeIndex
24552445
"""
2456-
from pandas import Timedelta
24572446
nanos = (roll % 2) * Timedelta(days=self.day_of_month - 1).value
24582447
return dtindex + nanos.astype("timedelta64[ns]")
24592448

@@ -2545,7 +2534,6 @@ cdef class Week(SingleConstructorOffset):
25452534
-------
25462535
result : DatetimeIndex
25472536
"""
2548-
from pandas import Timedelta
25492537
from .frequencies import get_freq_code # TODO: avoid circular import
25502538

25512539
i8other = dtindex.asi8
@@ -2847,8 +2835,6 @@ cdef class FY5253(FY5253Mixin):
28472835

28482836
@apply_wraps
28492837
def apply(self, other):
2850-
from pandas import Timestamp
2851-
28522838
norm = Timestamp(other).normalize()
28532839

28542840
n = self.n
@@ -3069,8 +3055,6 @@ cdef class FY5253Quarter(FY5253Mixin):
30693055
num_qtrs : int
30703056
tdelta : Timedelta
30713057
"""
3072-
from pandas import Timestamp, Timedelta
3073-
30743058
num_qtrs = 0
30753059

30763060
norm = Timestamp(other).tz_localize(None)
@@ -3101,7 +3085,6 @@ cdef class FY5253Quarter(FY5253Mixin):
31013085
@apply_wraps
31023086
def apply(self, other):
31033087
# Note: self.n == 0 is not allowed.
3104-
from pandas import Timedelta
31053088

31063089
n = self.n
31073090

@@ -3141,8 +3124,6 @@ cdef class FY5253Quarter(FY5253Mixin):
31413124
def year_has_extra_week(self, dt: datetime) -> bool:
31423125
# Avoid round-down errors --> normalize to get
31433126
# e.g. '370D' instead of '360D23H'
3144-
from pandas import Timestamp
3145-
31463127
norm = Timestamp(dt).normalize().tz_localize(None)
31473128

31483129
next_year_end = self._offset.rollforward(norm)
@@ -3621,9 +3602,6 @@ cpdef to_offset(freq):
36213602
>>> to_offset(Hour())
36223603
<Hour>
36233604
"""
3624-
# TODO: avoid runtime imports
3625-
from pandas._libs.tslibs.timedeltas import Timedelta
3626-
36273605
if freq is None:
36283606
return None
36293607

pandas/_libs/tslibs/tzconversion.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ from pandas._libs.tslibs.ccalendar cimport DAY_NANOS, HOUR_NANOS
2020
from pandas._libs.tslibs.nattype cimport NPY_NAT
2121
from pandas._libs.tslibs.np_datetime cimport (
2222
npy_datetimestruct, dt64_to_dtstruct)
23-
from pandas._libs.tslibs.timedeltas cimport delta_to_nanoseconds
2423
from pandas._libs.tslibs.timezones cimport (
2524
get_dst_info, is_tzlocal, is_utc, get_timezone, get_utcoffset)
2625

@@ -123,6 +122,7 @@ timedelta-like}
123122
elif nonexistent == 'shift_backward':
124123
shift_backward = True
125124
elif PyDelta_Check(nonexistent):
125+
from .timedeltas import delta_to_nanoseconds
126126
shift_delta = delta_to_nanoseconds(nonexistent)
127127
elif nonexistent not in ('raise', None):
128128
msg = ("nonexistent must be one of {'NaT', 'raise', 'shift_forward', "

0 commit comments

Comments
 (0)