1
1
from datetime import timedelta
2
2
import re
3
- from typing import Dict
3
+ from typing import Dict , Optional
4
4
5
5
import numpy as np
6
6
from pytz import AmbiguousTimeError
52
52
_offset_map : Dict [str , DateOffset ] = {}
53
53
54
54
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
+ """
57
59
return _offset_to_period_map .get (offset_str , None )
58
60
59
61
@@ -68,7 +70,7 @@ def get_period_alias(offset_str):
68
70
}
69
71
70
72
71
- def to_offset (freq ):
73
+ def to_offset (freq ) -> Optional [ DateOffset ] :
72
74
"""
73
75
Return DateOffset object from string or tuple representation
74
76
or datetime.timedelta object.
@@ -179,9 +181,9 @@ def to_offset(freq):
179
181
return delta
180
182
181
183
182
- def get_offset (name ) :
184
+ def get_offset (name : str ) -> DateOffset :
183
185
"""
184
- Return DateOffset object associated with rule name
186
+ Return DateOffset object associated with rule name.
185
187
186
188
Examples
187
189
--------
@@ -214,7 +216,7 @@ def get_offset(name):
214
216
# Period codes
215
217
216
218
217
- def infer_freq (index , warn = True ):
219
+ def infer_freq (index , warn : bool = True ) -> Optional [ str ] :
218
220
"""
219
221
Infer the most likely frequency given the input index. If the frequency is
220
222
uncertain, a warning will be printed.
@@ -247,6 +249,7 @@ def infer_freq(index, warn=True):
247
249
)
248
250
index = values
249
251
252
+ inferer : _FrequencyInferer
250
253
if is_period_arraylike (index ):
251
254
raise TypeError (
252
255
"PeriodIndex given. Check the `freq` attribute "
@@ -280,7 +283,7 @@ class _FrequencyInferer:
280
283
Not sure if I can avoid the state machine here
281
284
"""
282
285
283
- def __init__ (self , index , warn = True ):
286
+ def __init__ (self , index , warn : bool = True ):
284
287
self .index = index
285
288
self .values = index .asi8
286
289
@@ -315,7 +318,7 @@ def is_unique(self) -> bool:
315
318
def is_unique_asi8 (self ):
316
319
return len (self .deltas_asi8 ) == 1
317
320
318
- def get_freq (self ):
321
+ def get_freq (self ) -> Optional [ str ] :
319
322
"""
320
323
Find the appropriate frequency string to describe the inferred
321
324
frequency of self.values
@@ -388,7 +391,7 @@ def mdiffs(self):
388
391
def ydiffs (self ):
389
392
return unique_deltas (self .fields ["Y" ].astype ("i8" ))
390
393
391
- def _infer_daily_rule (self ):
394
+ def _infer_daily_rule (self ) -> Optional [ str ] :
392
395
annual_rule = self ._get_annual_rule ()
393
396
if annual_rule :
394
397
nyears = self .ydiffs [0 ]
@@ -424,7 +427,9 @@ def _infer_daily_rule(self):
424
427
if wom_rule :
425
428
return wom_rule
426
429
427
- def _get_annual_rule (self ):
430
+ return None
431
+
432
+ def _get_annual_rule (self ) -> Optional [str ]:
428
433
if len (self .ydiffs ) > 1 :
429
434
return None
430
435
@@ -434,7 +439,7 @@ def _get_annual_rule(self):
434
439
pos_check = self .month_position_check ()
435
440
return {"cs" : "AS" , "bs" : "BAS" , "ce" : "A" , "be" : "BA" }.get (pos_check )
436
441
437
- def _get_quarterly_rule (self ):
442
+ def _get_quarterly_rule (self ) -> Optional [ str ] :
438
443
if len (self .mdiffs ) > 1 :
439
444
return None
440
445
@@ -444,13 +449,13 @@ def _get_quarterly_rule(self):
444
449
pos_check = self .month_position_check ()
445
450
return {"cs" : "QS" , "bs" : "BQS" , "ce" : "Q" , "be" : "BQ" }.get (pos_check )
446
451
447
- def _get_monthly_rule (self ):
452
+ def _get_monthly_rule (self ) -> Optional [ str ] :
448
453
if len (self .mdiffs ) > 1 :
449
454
return None
450
455
pos_check = self .month_position_check ()
451
456
return {"cs" : "MS" , "bs" : "BMS" , "ce" : "M" , "be" : "BM" }.get (pos_check )
452
457
453
- def _is_business_daily (self ):
458
+ def _is_business_daily (self ) -> bool :
454
459
# quick check: cannot be business daily
455
460
if self .day_deltas != [1 , 3 ]:
456
461
return False
@@ -465,7 +470,7 @@ def _is_business_daily(self):
465
470
| ((weekdays > 0 ) & (weekdays <= 4 ) & (shifts == 1 ))
466
471
)
467
472
468
- def _get_wom_rule (self ):
473
+ def _get_wom_rule (self ) -> Optional [ str ] :
469
474
# wdiffs = unique(np.diff(self.index.week))
470
475
# We also need -47, -49, -48 to catch index spanning year boundary
471
476
# if not lib.ismember(wdiffs, set([4, 5, -47, -49, -48])).all():
@@ -501,11 +506,11 @@ def _infer_daily_rule(self):
501
506
return _maybe_add_count ("D" , days )
502
507
503
508
504
- def _is_multiple (us , mult ) :
509
+ def _is_multiple (us , mult : int ) -> bool :
505
510
return us % mult == 0
506
511
507
512
508
- def _maybe_add_count (base , count ) :
513
+ def _maybe_add_count (base : str , count : float ) -> str :
509
514
if count != 1 :
510
515
assert count == int (count )
511
516
count = int (count )
0 commit comments