Skip to content

Commit 9e95ef6

Browse files
simonjayhawkinsproost
authored andcommitted
TYP: some (mainly primative) types for tseries.frequencies (pandas-dev#29999)
1 parent da05f07 commit 9e95ef6

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

pandas/tseries/frequencies.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import timedelta
22
import re
3-
from typing import Dict
3+
from typing import Dict, Optional
44

55
import numpy as np
66
from pytz import AmbiguousTimeError
@@ -52,8 +52,10 @@
5252
_offset_map: Dict[str, DateOffset] = {}
5353

5454

55-
def get_period_alias(offset_str):
56-
""" alias to closest period strings BQ->Q etc"""
55+
def get_period_alias(offset_str: str) -> Optional[str]:
56+
"""
57+
Alias to closest period strings BQ->Q etc.
58+
"""
5759
return _offset_to_period_map.get(offset_str, None)
5860

5961

@@ -68,7 +70,7 @@ def get_period_alias(offset_str):
6870
}
6971

7072

71-
def to_offset(freq):
73+
def to_offset(freq) -> Optional[DateOffset]:
7274
"""
7375
Return DateOffset object from string or tuple representation
7476
or datetime.timedelta object.
@@ -179,9 +181,9 @@ def to_offset(freq):
179181
return delta
180182

181183

182-
def get_offset(name):
184+
def get_offset(name: str) -> DateOffset:
183185
"""
184-
Return DateOffset object associated with rule name
186+
Return DateOffset object associated with rule name.
185187
186188
Examples
187189
--------
@@ -214,7 +216,7 @@ def get_offset(name):
214216
# Period codes
215217

216218

217-
def infer_freq(index, warn=True):
219+
def infer_freq(index, warn: bool = True) -> Optional[str]:
218220
"""
219221
Infer the most likely frequency given the input index. If the frequency is
220222
uncertain, a warning will be printed.
@@ -247,6 +249,7 @@ def infer_freq(index, warn=True):
247249
)
248250
index = values
249251

252+
inferer: _FrequencyInferer
250253
if is_period_arraylike(index):
251254
raise TypeError(
252255
"PeriodIndex given. Check the `freq` attribute "
@@ -280,7 +283,7 @@ class _FrequencyInferer:
280283
Not sure if I can avoid the state machine here
281284
"""
282285

283-
def __init__(self, index, warn=True):
286+
def __init__(self, index, warn: bool = True):
284287
self.index = index
285288
self.values = index.asi8
286289

@@ -315,7 +318,7 @@ def is_unique(self) -> bool:
315318
def is_unique_asi8(self):
316319
return len(self.deltas_asi8) == 1
317320

318-
def get_freq(self):
321+
def get_freq(self) -> Optional[str]:
319322
"""
320323
Find the appropriate frequency string to describe the inferred
321324
frequency of self.values
@@ -388,7 +391,7 @@ def mdiffs(self):
388391
def ydiffs(self):
389392
return unique_deltas(self.fields["Y"].astype("i8"))
390393

391-
def _infer_daily_rule(self):
394+
def _infer_daily_rule(self) -> Optional[str]:
392395
annual_rule = self._get_annual_rule()
393396
if annual_rule:
394397
nyears = self.ydiffs[0]
@@ -424,7 +427,9 @@ def _infer_daily_rule(self):
424427
if wom_rule:
425428
return wom_rule
426429

427-
def _get_annual_rule(self):
430+
return None
431+
432+
def _get_annual_rule(self) -> Optional[str]:
428433
if len(self.ydiffs) > 1:
429434
return None
430435

@@ -434,7 +439,7 @@ def _get_annual_rule(self):
434439
pos_check = self.month_position_check()
435440
return {"cs": "AS", "bs": "BAS", "ce": "A", "be": "BA"}.get(pos_check)
436441

437-
def _get_quarterly_rule(self):
442+
def _get_quarterly_rule(self) -> Optional[str]:
438443
if len(self.mdiffs) > 1:
439444
return None
440445

@@ -444,13 +449,13 @@ def _get_quarterly_rule(self):
444449
pos_check = self.month_position_check()
445450
return {"cs": "QS", "bs": "BQS", "ce": "Q", "be": "BQ"}.get(pos_check)
446451

447-
def _get_monthly_rule(self):
452+
def _get_monthly_rule(self) -> Optional[str]:
448453
if len(self.mdiffs) > 1:
449454
return None
450455
pos_check = self.month_position_check()
451456
return {"cs": "MS", "bs": "BMS", "ce": "M", "be": "BM"}.get(pos_check)
452457

453-
def _is_business_daily(self):
458+
def _is_business_daily(self) -> bool:
454459
# quick check: cannot be business daily
455460
if self.day_deltas != [1, 3]:
456461
return False
@@ -465,7 +470,7 @@ def _is_business_daily(self):
465470
| ((weekdays > 0) & (weekdays <= 4) & (shifts == 1))
466471
)
467472

468-
def _get_wom_rule(self):
473+
def _get_wom_rule(self) -> Optional[str]:
469474
# wdiffs = unique(np.diff(self.index.week))
470475
# We also need -47, -49, -48 to catch index spanning year boundary
471476
# if not lib.ismember(wdiffs, set([4, 5, -47, -49, -48])).all():
@@ -501,11 +506,11 @@ def _infer_daily_rule(self):
501506
return _maybe_add_count("D", days)
502507

503508

504-
def _is_multiple(us, mult):
509+
def _is_multiple(us, mult: int) -> bool:
505510
return us % mult == 0
506511

507512

508-
def _maybe_add_count(base, count):
513+
def _maybe_add_count(base: str, count: float) -> str:
509514
if count != 1:
510515
assert count == int(count)
511516
count = int(count)

0 commit comments

Comments
 (0)