Skip to content

Commit 940695b

Browse files
authored
TYP: stubs for tslibs (#40433)
1 parent b172a60 commit 940695b

File tree

7 files changed

+136
-3
lines changed

7 files changed

+136
-3
lines changed

pandas/_libs/tslibs/ccalendar.pyi

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
DAYS: list[str]
3+
MONTH_ALIASES: dict[int, str]
4+
MONTH_NUMBERS: dict[str, int]
5+
MONTHS: list[str]
6+
int_to_weekday: dict[int, str]
7+
8+
def get_firstbday(year: int, month: int) -> int: ...
9+
def get_lastbday(year: int, month: int) -> int: ...
10+
def get_day_of_year(year: int, month: int, day: int) -> int: ...
11+
def get_iso_calendar(year: int, month: int, day: int) -> tuple[int, int, int]: ...
12+
def get_week_of_year(year: int, month: int, day: int) -> int: ...
13+
def get_days_in_month(year: int, month: int) -> int: ...

pandas/_libs/tslibs/strptime.pyi

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import Optional
2+
3+
import numpy as np
4+
5+
def array_strptime(
6+
values: np.ndarray, # np.ndarray[object]
7+
fmt: Optional[str],
8+
exact: bool = True,
9+
errors: str = "raise"
10+
) -> tuple[np.ndarray, np.ndarray]: ...
11+
# first ndarray is M8[ns], second is object ndarray of Optional[tzinfo]

pandas/_libs/tslibs/timezones.pyi

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from datetime import (
2+
datetime,
3+
tzinfo,
4+
)
5+
from typing import (
6+
Callable,
7+
Optional,
8+
Union,
9+
)
10+
11+
import numpy as np
12+
13+
# imported from dateutil.tz
14+
dateutil_gettz: Callable[[str], tzinfo]
15+
16+
17+
def tz_standardize(tz: tzinfo) -> tzinfo: ...
18+
19+
def tz_compare(start: Optional[tzinfo], end: Optional[tzinfo]) -> bool: ...
20+
21+
def infer_tzinfo(
22+
start: Optional[datetime], end: Optional[datetime],
23+
) -> Optional[tzinfo]: ...
24+
25+
# ndarrays returned are both int64_t
26+
def get_dst_info(tz: tzinfo) -> tuple[np.ndarray, np.ndarray, str]: ...
27+
28+
def maybe_get_tz(tz: Optional[Union[str, int, np.int64, tzinfo]]) -> Optional[tzinfo]: ...
29+
30+
def get_timezone(tz: tzinfo) -> Union[tzinfo, str]: ...
31+
32+
def is_utc(tz: Optional[tzinfo]) -> bool: ...

pandas/_libs/tslibs/timezones.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ cdef inline bint treat_tz_as_dateutil(tzinfo tz):
6767
return hasattr(tz, '_trans_list') and hasattr(tz, '_trans_idx')
6868

6969

70+
# Returns str or tzinfo object
7071
cpdef inline object get_timezone(tzinfo tz):
7172
"""
7273
We need to do several things here:
@@ -80,6 +81,8 @@ cpdef inline object get_timezone(tzinfo tz):
8081
the tz name. It needs to be a string so that we can serialize it with
8182
UJSON/pytables. maybe_get_tz (below) is the inverse of this process.
8283
"""
84+
if tz is None:
85+
raise TypeError("tz argument cannot be None")
8386
if is_utc(tz):
8487
return tz
8588
else:
@@ -364,6 +367,8 @@ cpdef bint tz_compare(tzinfo start, tzinfo end):
364367
elif is_utc(end):
365368
# Ensure we don't treat tzlocal as equal to UTC when running in UTC
366369
return False
370+
elif start is None or end is None:
371+
return start is None and end is None
367372
return get_timezone(start) == get_timezone(end)
368373

369374

pandas/_libs/tslibs/tzconversion.pyi

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from datetime import (
2+
timedelta,
3+
tzinfo,
4+
)
5+
from typing import (
6+
Iterable,
7+
Optional,
8+
Union,
9+
)
10+
11+
import numpy as np
12+
13+
def tz_convert_from_utc(
14+
vals: np.ndarray, # const int64_t[:]
15+
tz: tzinfo,
16+
) -> np.ndarray: ... # np.ndarray[np.int64]
17+
18+
def tz_convert_from_utc_single(val: np.int64, tz: tzinfo) -> np.int64: ...
19+
20+
def tz_localize_to_utc(
21+
vals: np.ndarray, # np.ndarray[np.int64]
22+
tz: Optional[tzinfo],
23+
ambiguous: Optional[Union[str, bool, Iterable[bool]]] = None,
24+
nonexistent: Optional[Union[str, timedelta, np.timedelta64]] = None,
25+
) -> np.ndarray: ... # np.ndarray[np.int64]

pandas/_libs/tslibs/vectorized.pyi

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
For cython types that cannot be represented precisely, closest-available
3+
python equivalents are used, and the precise types kept as adjacent comments.
4+
"""
5+
from datetime import tzinfo
6+
from typing import (
7+
Optional,
8+
Union,
9+
)
10+
11+
import numpy as np
12+
13+
from pandas._libs.tslibs.dtypes import Resolution
14+
from pandas._libs.tslibs.offsets import BaseOffset
15+
16+
def dt64arr_to_periodarr(
17+
stamps: np.ndarray, # const int64_t[:]
18+
freq: int,
19+
tz: Optional[tzinfo],
20+
) -> np.ndarray: ... # np.ndarray[np.int64, ndim=1]
21+
22+
23+
def is_date_array_normalized(
24+
stamps: np.ndarray, # const int64_t[:]
25+
tz: Optional[tzinfo] = None,
26+
) -> bool: ...
27+
28+
29+
def normalize_i8_timestamps(
30+
stamps: np.ndarray, # const int64_t[:]
31+
tz: Optional[tzinfo],
32+
) -> np.ndarray: ... # np.ndarray[np.int64]
33+
34+
35+
def get_resolution(
36+
stamps: np.ndarray, # const int64_t[:]
37+
tz: Optional[tzinfo] = None,
38+
) -> Resolution: ...
39+
40+
41+
def ints_to_pydatetime(
42+
arr: np.ndarray, # const int64_t[:}]
43+
tz: Optional[tzinfo] = None,
44+
freq: Optional[Union[str, BaseOffset]] = None,
45+
fold: bool = False,
46+
box: str = "datetime",
47+
) -> np.ndarray: ... # np.ndarray[object]

pandas/core/tools/datetimes.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def _convert_listlike_datetimes(
289289
name: Hashable = None,
290290
tz: Optional[Timezone] = None,
291291
unit: Optional[str] = None,
292-
errors: Optional[str] = None,
292+
errors: str = "raise",
293293
infer_datetime_format: bool = False,
294294
dayfirst: Optional[bool] = None,
295295
yearfirst: Optional[bool] = None,
@@ -432,7 +432,7 @@ def _array_strptime_with_fallback(
432432
tz,
433433
fmt: str,
434434
exact: bool,
435-
errors: Optional[str],
435+
errors: str,
436436
infer_datetime_format: bool,
437437
) -> Optional[Index]:
438438
"""
@@ -480,7 +480,7 @@ def _to_datetime_with_format(
480480
tz,
481481
fmt: str,
482482
exact: bool,
483-
errors: Optional[str],
483+
errors: str,
484484
infer_datetime_format: bool,
485485
) -> Optional[Index]:
486486
"""

0 commit comments

Comments
 (0)