Skip to content

Commit f887eb0

Browse files
jbrockmendelsimonjayhawkins
authored andcommitted
TYP: offsets (#30897)
1 parent 0b4bac7 commit f887eb0

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

pandas/tseries/offsets.py

+45-45
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def apply_index(self, i):
365365
"applied vectorized"
366366
)
367367

368-
def is_anchored(self):
368+
def is_anchored(self) -> bool:
369369
# TODO: Does this make sense for the general case? It would help
370370
# if there were a canonical docstring for what is_anchored means.
371371
return self.n == 1
@@ -378,7 +378,7 @@ def onOffset(self, dt):
378378
)
379379
return self.is_on_offset(dt)
380380

381-
def isAnchored(self):
381+
def isAnchored(self) -> bool:
382382
warnings.warn(
383383
"isAnchored is a deprecated, use is_anchored instead",
384384
FutureWarning,
@@ -389,7 +389,7 @@ def isAnchored(self):
389389
# TODO: Combine this with BusinessMixin version by defining a whitelisted
390390
# set of attributes on each object rather than the existing behavior of
391391
# iterating over internal ``__dict__``
392-
def _repr_attrs(self):
392+
def _repr_attrs(self) -> str:
393393
exclude = {"n", "inc", "normalize"}
394394
attrs = []
395395
for attr in sorted(self.__dict__):
@@ -405,7 +405,7 @@ def _repr_attrs(self):
405405
return out
406406

407407
@property
408-
def name(self):
408+
def name(self) -> str:
409409
return self.rule_code
410410

411411
def rollback(self, dt):
@@ -452,15 +452,15 @@ def is_on_offset(self, dt):
452452

453453
# way to get around weirdness with rule_code
454454
@property
455-
def _prefix(self):
455+
def _prefix(self) -> str:
456456
raise NotImplementedError("Prefix not defined")
457457

458458
@property
459-
def rule_code(self):
459+
def rule_code(self) -> str:
460460
return self._prefix
461461

462462
@cache_readonly
463-
def freqstr(self):
463+
def freqstr(self) -> str:
464464
try:
465465
code = self.rule_code
466466
except NotImplementedError:
@@ -480,7 +480,7 @@ def freqstr(self):
480480

481481
return fstr
482482

483-
def _offset_str(self):
483+
def _offset_str(self) -> str:
484484
return ""
485485

486486
@property
@@ -529,11 +529,11 @@ def offset(self):
529529
# Alias for backward compat
530530
return self._offset
531531

532-
def _repr_attrs(self):
532+
def _repr_attrs(self) -> str:
533533
if self.offset:
534534
attrs = [f"offset={repr(self.offset)}"]
535535
else:
536-
attrs = None
536+
attrs = []
537537
out = ""
538538
if attrs:
539539
out += ": " + ", ".join(attrs)
@@ -553,7 +553,7 @@ def __init__(self, n=1, normalize=False, offset=timedelta(0)):
553553
BaseOffset.__init__(self, n, normalize)
554554
object.__setattr__(self, "_offset", offset)
555555

556-
def _offset_str(self):
556+
def _offset_str(self) -> str:
557557
def get_str(td):
558558
off_str = ""
559559
if td.days > 0:
@@ -649,7 +649,7 @@ def apply_index(self, i):
649649
result = shifted.to_timestamp() + time
650650
return result
651651

652-
def is_on_offset(self, dt):
652+
def is_on_offset(self, dt: datetime) -> bool:
653653
if self.normalize and not _is_normalized(dt):
654654
return False
655655
return dt.weekday() < 5
@@ -1087,7 +1087,7 @@ def apply(self, other):
10871087
def apply_index(self, i):
10881088
raise NotImplementedError
10891089

1090-
def is_on_offset(self, dt):
1090+
def is_on_offset(self, dt: datetime) -> bool:
10911091
if self.normalize and not _is_normalized(dt):
10921092
return False
10931093
day64 = _to_dt64(dt, "datetime64[D]")
@@ -1134,14 +1134,14 @@ class MonthOffset(SingleConstructorOffset):
11341134
__init__ = BaseOffset.__init__
11351135

11361136
@property
1137-
def name(self):
1137+
def name(self) -> str:
11381138
if self.is_anchored:
11391139
return self.rule_code
11401140
else:
11411141
month = ccalendar.MONTH_ALIASES[self.n]
11421142
return f"{self.code_rule}-{month}"
11431143

1144-
def is_on_offset(self, dt):
1144+
def is_on_offset(self, dt: datetime) -> bool:
11451145
if self.normalize and not _is_normalized(dt):
11461146
return False
11471147
return dt.day == self._get_offset_day(dt)
@@ -1333,7 +1333,7 @@ def _from_name(cls, suffix=None):
13331333
return cls(day_of_month=suffix)
13341334

13351335
@property
1336-
def rule_code(self):
1336+
def rule_code(self) -> str:
13371337
suffix = f"-{self.day_of_month}"
13381338
return self._prefix + suffix
13391339

@@ -1429,7 +1429,7 @@ class SemiMonthEnd(SemiMonthOffset):
14291429
_prefix = "SM"
14301430
_min_day_of_month = 1
14311431

1432-
def is_on_offset(self, dt):
1432+
def is_on_offset(self, dt: datetime) -> bool:
14331433
if self.normalize and not _is_normalized(dt):
14341434
return False
14351435
days_in_month = ccalendar.get_days_in_month(dt.year, dt.month)
@@ -1487,7 +1487,7 @@ class SemiMonthBegin(SemiMonthOffset):
14871487

14881488
_prefix = "SMS"
14891489

1490-
def is_on_offset(self, dt):
1490+
def is_on_offset(self, dt: datetime) -> bool:
14911491
if self.normalize and not _is_normalized(dt):
14921492
return False
14931493
return dt.day in (1, self.day_of_month)
@@ -1556,7 +1556,7 @@ def __init__(self, n=1, normalize=False, weekday=None):
15561556
if self.weekday < 0 or self.weekday > 6:
15571557
raise ValueError(f"Day must be 0<=day<=6, got {self.weekday}")
15581558

1559-
def is_anchored(self):
1559+
def is_anchored(self) -> bool:
15601560
return self.n == 1 and self.weekday is not None
15611561

15621562
@apply_wraps
@@ -1632,15 +1632,15 @@ def _end_apply_index(self, dtindex):
16321632

16331633
return base + off + Timedelta(1, "ns") - Timedelta(1, "D")
16341634

1635-
def is_on_offset(self, dt):
1635+
def is_on_offset(self, dt: datetime) -> bool:
16361636
if self.normalize and not _is_normalized(dt):
16371637
return False
16381638
elif self.weekday is None:
16391639
return True
16401640
return dt.weekday() == self.weekday
16411641

16421642
@property
1643-
def rule_code(self):
1643+
def rule_code(self) -> str:
16441644
suffix = ""
16451645
if self.weekday is not None:
16461646
weekday = ccalendar.int_to_weekday[self.weekday]
@@ -1717,7 +1717,7 @@ def __init__(self, n=1, normalize=False, week=0, weekday=0):
17171717
if self.week < 0 or self.week > 3:
17181718
raise ValueError(f"Week must be 0<=week<=3, got {self.week}")
17191719

1720-
def _get_offset_day(self, other):
1720+
def _get_offset_day(self, other: datetime) -> int:
17211721
"""
17221722
Find the day in the same month as other that has the same
17231723
weekday as self.weekday and is the self.week'th such day in the month.
@@ -1736,7 +1736,7 @@ def _get_offset_day(self, other):
17361736
return 1 + shift_days + self.week * 7
17371737

17381738
@property
1739-
def rule_code(self):
1739+
def rule_code(self) -> str:
17401740
weekday = ccalendar.int_to_weekday.get(self.weekday, "")
17411741
return f"{self._prefix}-{self.week + 1}{weekday}"
17421742

@@ -1785,7 +1785,7 @@ def __init__(self, n=1, normalize=False, weekday=0):
17851785
if self.weekday < 0 or self.weekday > 6:
17861786
raise ValueError(f"Day must be 0<=day<=6, got {self.weekday}")
17871787

1788-
def _get_offset_day(self, other):
1788+
def _get_offset_day(self, other: datetime) -> int:
17891789
"""
17901790
Find the day in the same month as other that has the same
17911791
weekday as self.weekday and is the last such day in the month.
@@ -1805,7 +1805,7 @@ def _get_offset_day(self, other):
18051805
return dim - shift_days
18061806

18071807
@property
1808-
def rule_code(self):
1808+
def rule_code(self) -> str:
18091809
weekday = ccalendar.int_to_weekday.get(self.weekday, "")
18101810
return f"{self._prefix}-{weekday}"
18111811

@@ -1842,7 +1842,7 @@ def __init__(self, n=1, normalize=False, startingMonth=None):
18421842
startingMonth = self._default_startingMonth
18431843
object.__setattr__(self, "startingMonth", startingMonth)
18441844

1845-
def is_anchored(self):
1845+
def is_anchored(self) -> bool:
18461846
return self.n == 1 and self.startingMonth is not None
18471847

18481848
@classmethod
@@ -1856,7 +1856,7 @@ def _from_name(cls, suffix=None):
18561856
return cls(**kwargs)
18571857

18581858
@property
1859-
def rule_code(self):
1859+
def rule_code(self) -> str:
18601860
month = ccalendar.MONTH_ALIASES[self.startingMonth]
18611861
return f"{self._prefix}-{month}"
18621862

@@ -1874,7 +1874,7 @@ def apply(self, other):
18741874
months = qtrs * 3 - months_since
18751875
return shift_month(other, months, self._day_opt)
18761876

1877-
def is_on_offset(self, dt):
1877+
def is_on_offset(self, dt: datetime) -> bool:
18781878
if self.normalize and not _is_normalized(dt):
18791879
return False
18801880
mod_month = (dt.month - self.startingMonth) % 3
@@ -1953,7 +1953,7 @@ class YearOffset(DateOffset):
19531953
_adjust_dst = True
19541954
_attributes = frozenset(["n", "normalize", "month"])
19551955

1956-
def _get_offset_day(self, other):
1956+
def _get_offset_day(self, other: datetime) -> int:
19571957
# override BaseOffset method to use self.month instead of other.month
19581958
# TODO: there may be a more performant way to do this
19591959
return liboffsets.get_day_of_month(
@@ -1977,7 +1977,7 @@ def apply_index(self, dtindex):
19771977
shifted, freq=dtindex.freq, dtype=dtindex.dtype
19781978
)
19791979

1980-
def is_on_offset(self, dt):
1980+
def is_on_offset(self, dt: datetime) -> bool:
19811981
if self.normalize and not _is_normalized(dt):
19821982
return False
19831983
return dt.month == self.month and dt.day == self._get_offset_day(dt)
@@ -1999,7 +1999,7 @@ def _from_name(cls, suffix=None):
19991999
return cls(**kwargs)
20002000

20012001
@property
2002-
def rule_code(self):
2002+
def rule_code(self) -> str:
20032003
month = ccalendar.MONTH_ALIASES[self.month]
20042004
return f"{self._prefix}-{month}"
20052005

@@ -2117,12 +2117,12 @@ def __init__(
21172117
if self.variation not in ["nearest", "last"]:
21182118
raise ValueError(f"{self.variation} is not a valid variation")
21192119

2120-
def is_anchored(self):
2120+
def is_anchored(self) -> bool:
21212121
return (
21222122
self.n == 1 and self.startingMonth is not None and self.weekday is not None
21232123
)
21242124

2125-
def is_on_offset(self, dt):
2125+
def is_on_offset(self, dt: datetime) -> bool:
21262126
if self.normalize and not _is_normalized(dt):
21272127
return False
21282128
dt = datetime(dt.year, dt.month, dt.day)
@@ -2217,18 +2217,18 @@ def get_year_end(self, dt):
22172217
return target_date + timedelta(days_forward - 7)
22182218

22192219
@property
2220-
def rule_code(self):
2220+
def rule_code(self) -> str:
22212221
prefix = self._prefix
22222222
suffix = self.get_rule_code_suffix()
22232223
return f"{prefix}-{suffix}"
22242224

2225-
def _get_suffix_prefix(self):
2225+
def _get_suffix_prefix(self) -> str:
22262226
if self.variation == "nearest":
22272227
return "N"
22282228
else:
22292229
return "L"
22302230

2231-
def get_rule_code_suffix(self):
2231+
def get_rule_code_suffix(self) -> str:
22322232
prefix = self._get_suffix_prefix()
22332233
month = ccalendar.MONTH_ALIASES[self.startingMonth]
22342234
weekday = ccalendar.int_to_weekday[self.weekday]
@@ -2346,7 +2346,7 @@ def _offset(self):
23462346
variation=self.variation,
23472347
)
23482348

2349-
def is_anchored(self):
2349+
def is_anchored(self) -> bool:
23502350
return self.n == 1 and self._offset.is_anchored()
23512351

23522352
def _rollback_to_year(self, other):
@@ -2434,7 +2434,7 @@ def get_weeks(self, dt):
24342434

24352435
return ret
24362436

2437-
def year_has_extra_week(self, dt):
2437+
def year_has_extra_week(self, dt: datetime) -> bool:
24382438
# Avoid round-down errors --> normalize to get
24392439
# e.g. '370D' instead of '360D23H'
24402440
norm = Timestamp(dt).normalize().tz_localize(None)
@@ -2445,7 +2445,7 @@ def year_has_extra_week(self, dt):
24452445
assert weeks_in_year in [52, 53], weeks_in_year
24462446
return weeks_in_year == 53
24472447

2448-
def is_on_offset(self, dt):
2448+
def is_on_offset(self, dt: datetime) -> bool:
24492449
if self.normalize and not _is_normalized(dt):
24502450
return False
24512451
if self._offset.is_on_offset(dt):
@@ -2463,7 +2463,7 @@ def is_on_offset(self, dt):
24632463
return False
24642464

24652465
@property
2466-
def rule_code(self):
2466+
def rule_code(self) -> str:
24672467
suffix = self._offset.get_rule_code_suffix()
24682468
qtr = self.qtr_with_extra_week
24692469
return f"{self._prefix}-{suffix}-{qtr}"
@@ -2516,7 +2516,7 @@ def apply(self, other):
25162516
)
25172517
return new
25182518

2519-
def is_on_offset(self, dt):
2519+
def is_on_offset(self, dt: datetime) -> bool:
25202520
if self.normalize and not _is_normalized(dt):
25212521
return False
25222522
return date(dt.year, dt.month, dt.day) == easter(dt.year)
@@ -2596,7 +2596,7 @@ def __eq__(self, other: Any) -> bool:
25962596

25972597
# This is identical to DateOffset.__hash__, but has to be redefined here
25982598
# for Python 3, because we've redefined __eq__.
2599-
def __hash__(self):
2599+
def __hash__(self) -> int:
26002600
return hash(self._params)
26012601

26022602
def __ne__(self, other):
@@ -2617,7 +2617,7 @@ def __ne__(self, other):
26172617
return True
26182618

26192619
@property
2620-
def delta(self):
2620+
def delta(self) -> Timedelta:
26212621
return self.n * self._inc
26222622

26232623
@property
@@ -2648,11 +2648,11 @@ def apply(self, other):
26482648

26492649
raise ApplyTypeError(f"Unhandled type: {type(other).__name__}")
26502650

2651-
def is_anchored(self):
2651+
def is_anchored(self) -> bool:
26522652
return False
26532653

26542654

2655-
def _delta_to_tick(delta):
2655+
def _delta_to_tick(delta: timedelta) -> Tick:
26562656
if delta.microseconds == 0 and getattr(delta, "nanoseconds", 0) == 0:
26572657
# nanoseconds only for pd.Timedelta
26582658
if delta.seconds == 0:

0 commit comments

Comments
 (0)