Skip to content

Commit 7efbcac

Browse files
authored
CLN: liboffsets annotations (#34815)
1 parent 60ba05f commit 7efbcac

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

pandas/_libs/tslibs/offsets.pyx

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,6 @@ cdef class BaseOffset:
358358
Base class for DateOffset methods that are not overridden by subclasses
359359
and will (after pickle errors are resolved) go into a cdef class.
360360
"""
361-
_typ = "dateoffset"
362361
_day_opt = None
363362
_attributes = tuple(["n", "normalize"])
364363
_use_relativedelta = False
@@ -394,7 +393,7 @@ cdef class BaseOffset:
394393
def __ne__(self, other):
395394
return not self == other
396395

397-
def __hash__(self):
396+
def __hash__(self) -> int:
398397
return hash(self._params)
399398

400399
@cache_readonly
@@ -422,10 +421,10 @@ cdef class BaseOffset:
422421
return params
423422

424423
@property
425-
def kwds(self):
424+
def kwds(self) -> dict:
426425
# for backwards-compatibility
427426
kwds = {name: getattr(self, name, None) for name in self._attributes
428-
if name not in ['n', 'normalize']}
427+
if name not in ["n", "normalize"]}
429428
return {name: kwds[name] for name in kwds if kwds[name] is not None}
430429

431430
@property
@@ -582,7 +581,7 @@ cdef class BaseOffset:
582581
"does not have a vectorized implementation"
583582
)
584583

585-
def rollback(self, dt):
584+
def rollback(self, dt) -> datetime:
586585
"""
587586
Roll provided date backward to next offset only if not on offset.
588587

@@ -596,7 +595,7 @@ cdef class BaseOffset:
596595
dt = dt - type(self)(1, normalize=self.normalize, **self.kwds)
597596
return dt
598597

599-
def rollforward(self, dt):
598+
def rollforward(self, dt) -> datetime:
600599
"""
601600
Roll provided date forward to next offset only if not on offset.
602601

@@ -618,7 +617,7 @@ cdef class BaseOffset:
618617
pydate_to_dtstruct(other, &dts)
619618
return get_day_of_month(&dts, self._day_opt)
620619

621-
def is_on_offset(self, dt) -> bool:
620+
def is_on_offset(self, dt: datetime) -> bool:
622621
if self.normalize and not _is_normalized(dt):
623622
return False
624623

@@ -780,6 +779,8 @@ cdef class Tick(SingleConstructorOffset):
780779
def nanos(self) -> int64_t:
781780
return self.n * self._nanos_inc
782781

782+
# FIXME: This should be typed as datetime, but we DatetimeLikeIndex.insert
783+
# checks self.freq.is_on_offset with a Timedelta sometimes.
783784
def is_on_offset(self, dt) -> bool:
784785
return True
785786

@@ -861,16 +862,8 @@ cdef class Tick(SingleConstructorOffset):
861862
def apply(self, other):
862863
# Timestamp can handle tz and nano sec, thus no need to use apply_wraps
863864
if isinstance(other, _Timestamp):
864-
865865
# GH#15126
866-
# in order to avoid a recursive
867-
# call of __add__ and __radd__ if there is
868-
# an exception, when we call using the + operator,
869-
# we directly call the known method
870-
result = other.__add__(self)
871-
if result is NotImplemented:
872-
raise OverflowError
873-
return result
866+
return other + self.delta
874867
elif other is NaT:
875868
return NaT
876869
elif is_datetime64_object(other) or PyDate_Check(other):
@@ -1097,7 +1090,7 @@ cdef class RelativeDeltaOffset(BaseOffset):
10971090
"applied vectorized"
10981091
)
10991092

1100-
def is_on_offset(self, dt) -> bool:
1093+
def is_on_offset(self, dt: datetime) -> bool:
11011094
if self.normalize and not _is_normalized(dt):
11021095
return False
11031096
# TODO: see GH#1395
@@ -1384,7 +1377,7 @@ cdef class BusinessDay(BusinessMixin):
13841377
i8other = dtindex.view("i8")
13851378
return shift_bdays(i8other, self.n)
13861379

1387-
def is_on_offset(self, dt) -> bool:
1380+
def is_on_offset(self, dt: datetime) -> bool:
13881381
if self.normalize and not _is_normalized(dt):
13891382
return False
13901383
return dt.weekday() < 5
@@ -1788,7 +1781,7 @@ cdef class WeekOfMonthMixin(SingleConstructorOffset):
17881781
to_day = self._get_offset_day(shifted)
17891782
return shift_day(shifted, to_day - shifted.day)
17901783

1791-
def is_on_offset(self, dt) -> bool:
1784+
def is_on_offset(self, dt: datetime) -> bool:
17921785
if self.normalize and not _is_normalized(dt):
17931786
return False
17941787
return dt.day == self._get_offset_day(dt)
@@ -1843,12 +1836,12 @@ cdef class YearOffset(SingleConstructorOffset):
18431836
month = MONTH_ALIASES[self.month]
18441837
return f"{self._prefix}-{month}"
18451838

1846-
def is_on_offset(self, dt) -> bool:
1839+
def is_on_offset(self, dt: datetime) -> bool:
18471840
if self.normalize and not _is_normalized(dt):
18481841
return False
18491842
return dt.month == self.month and dt.day == self._get_offset_day(dt)
18501843

1851-
def _get_offset_day(self, other) -> int:
1844+
def _get_offset_day(self, other: datetime) -> int:
18521845
# override BaseOffset method to use self.month instead of other.month
18531846
cdef:
18541847
npy_datetimestruct dts
@@ -1995,7 +1988,7 @@ cdef class QuarterOffset(SingleConstructorOffset):
19951988
def is_anchored(self) -> bool:
19961989
return self.n == 1 and self.startingMonth is not None
19971990

1998-
def is_on_offset(self, dt) -> bool:
1991+
def is_on_offset(self, dt: datetime) -> bool:
19991992
if self.normalize and not _is_normalized(dt):
20001993
return False
20011994
mod_month = (dt.month - self.startingMonth) % 3
@@ -2119,7 +2112,7 @@ cdef class QuarterBegin(QuarterOffset):
21192112
# Month-Based Offset Classes
21202113

21212114
cdef class MonthOffset(SingleConstructorOffset):
2122-
def is_on_offset(self, dt) -> bool:
2115+
def is_on_offset(self, dt: datetime) -> bool:
21232116
if self.normalize and not _is_normalized(dt):
21242117
return False
21252118
return dt.day == self._get_offset_day(dt)
@@ -2339,7 +2332,7 @@ cdef class SemiMonthEnd(SemiMonthOffset):
23392332
_prefix = "SM"
23402333
_min_day_of_month = 1
23412334

2342-
def is_on_offset(self, dt) -> bool:
2335+
def is_on_offset(self, dt: datetime) -> bool:
23432336
if self.normalize and not _is_normalized(dt):
23442337
return False
23452338
days_in_month = get_days_in_month(dt.year, dt.month)
@@ -2360,7 +2353,7 @@ cdef class SemiMonthBegin(SemiMonthOffset):
23602353

23612354
_prefix = "SMS"
23622355

2363-
def is_on_offset(self, dt) -> bool:
2356+
def is_on_offset(self, dt: datetime) -> bool:
23642357
if self.normalize and not _is_normalized(dt):
23652358
return False
23662359
return dt.day in (1, self.day_of_month)
@@ -2375,8 +2368,8 @@ cdef class Week(SingleConstructorOffset):
23752368
Weekly offset.
23762369
23772370
Parameters
2378-
----------f
2379-
weekday : int, default None
2371+
----------
2372+
weekday : int or None, default None
23802373
Always generate specific day of week. 0 for Monday.
23812374
"""
23822375

0 commit comments

Comments
 (0)