Skip to content

Commit 447ef57

Browse files
authored
TYP: Subset of "Improved the type stubs in the _libs directory to help with type checking" (#44251)
1 parent 01f4b7b commit 447ef57

File tree

15 files changed

+105
-60
lines changed

15 files changed

+105
-60
lines changed

pandas/_libs/interval.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,9 @@ def intervals_to_interval_bounds(ndarray intervals, bint validate_closed=True):
516516
517517
Returns
518518
-------
519-
tuple of tuples
520-
left : (ndarray, object, array)
521-
right : (ndarray, object, array)
519+
tuple of
520+
left : ndarray
521+
right : ndarray
522522
closed: str
523523
"""
524524
cdef:

pandas/_libs/missing.pyi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import numpy as np
2+
from numpy import typing as npt
3+
4+
class NAType: ...
5+
6+
NA: NAType
7+
8+
def is_matching_na(
9+
left: object, right: object, nan_matches_none: bool = ...
10+
) -> bool: ...
11+
def isposinf_scalar(val: object) -> bool: ...
12+
def isneginf_scalar(val: object) -> bool: ...
13+
def checknull(val: object, inf_as_na: bool = ...) -> bool: ...
14+
def isnaobj(arr: np.ndarray, inf_as_na: bool = ...) -> npt.NDArray[np.bool_]: ...
15+
def is_numeric_na(values: np.ndarray) -> npt.NDArray[np.bool_]: ...

pandas/_libs/tslibs/dtypes.pyi

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,33 @@ class PeriodDtypeBase:
1818
def resolution(self) -> Resolution: ...
1919

2020
class FreqGroup(Enum):
21-
FR_ANN: int = ...
22-
FR_QTR: int = ...
23-
FR_MTH: int = ...
24-
FR_WK: int = ...
25-
FR_BUS: int = ...
26-
FR_DAY: int = ...
27-
FR_HR: int = ...
28-
FR_MIN: int = ...
29-
FR_SEC: int = ...
30-
FR_MS: int = ...
31-
FR_US: int = ...
32-
FR_NS: int = ...
33-
FR_UND: int = ...
21+
FR_ANN: int
22+
FR_QTR: int
23+
FR_MTH: int
24+
FR_WK: int
25+
FR_BUS: int
26+
FR_DAY: int
27+
FR_HR: int
28+
FR_MIN: int
29+
FR_SEC: int
30+
FR_MS: int
31+
FR_US: int
32+
FR_NS: int
33+
FR_UND: int
3434
@staticmethod
3535
def get_freq_group(code: int) -> FreqGroup: ...
3636

3737
class Resolution(Enum):
38-
RESO_NS: int = ...
39-
RESO_US: int = ...
40-
RESO_MS: int = ...
41-
RESO_SEC: int = ...
42-
RESO_MIN: int = ...
43-
RESO_HR: int = ...
44-
RESO_DAY: int = ...
45-
RESO_MTH: int = ...
46-
RESO_QTR: int = ...
47-
RESO_YR: int = ...
38+
RESO_NS: int
39+
RESO_US: int
40+
RESO_MS: int
41+
RESO_SEC: int
42+
RESO_MIN: int
43+
RESO_HR: int
44+
RESO_DAY: int
45+
RESO_MTH: int
46+
RESO_QTR: int
47+
RESO_YR: int
4848
def __lt__(self, other: Resolution) -> bool: ...
4949
def __ge__(self, other: Resolution) -> bool: ...
5050
@property

pandas/_libs/tslibs/nattype.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ NaT: NaTType
1212
iNaT: int
1313
nat_strings: set[str]
1414

15+
def is_null_datetimelike(val: object, inat_is_null: bool = ...) -> bool: ...
16+
1517
class NaTType(datetime):
1618
value: np.int64
1719
def asm8(self) -> np.datetime64: ...

pandas/_libs/tslibs/np_datetime.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class OutOfBoundsDatetime(ValueError): ...

pandas/_libs/tslibs/offsets.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3573,7 +3573,7 @@ cpdef to_offset(freq):
35733573
35743574
Parameters
35753575
----------
3576-
freq : str, tuple, datetime.timedelta, DateOffset or None
3576+
freq : str, datetime.timedelta, BaseOffset or None
35773577
35783578
Returns
35793579
-------
@@ -3586,7 +3586,7 @@ cpdef to_offset(freq):
35863586
35873587
See Also
35883588
--------
3589-
DateOffset : Standard kind of date increment used for a date range.
3589+
BaseOffset : Standard kind of date increment used for a date range.
35903590
35913591
Examples
35923592
--------

pandas/_libs/tslibs/timestamps.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ import numpy as np
1717

1818
from pandas._libs.tslibs import (
1919
BaseOffset,
20-
NaT,
2120
NaTType,
2221
Period,
2322
Timedelta,
2423
)
2524

2625
_S = TypeVar("_S")
2726

28-
def integer_op_not_supported(obj) -> None: ...
27+
def integer_op_not_supported(obj) -> TypeError: ...
2928

3029
class Timestamp(datetime):
3130
min: ClassVar[Timestamp]

pandas/core/arrays/string_.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,9 @@ def _cmp_method(self, other, op):
512512

513513
# ------------------------------------------------------------------------
514514
# String methods interface
515-
_str_na_value = StringDtype.na_value
515+
# error: Incompatible types in assignment (expression has type "NAType",
516+
# base class "PandasArray" defined the type as "float")
517+
_str_na_value = StringDtype.na_value # type: ignore[assignment]
516518

517519
def _str_map(
518520
self, f, na_value=None, dtype: Dtype | None = None, convert: bool = True

pandas/core/dtypes/cast.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,10 @@ def maybe_upcast(
912912
# We get a copy in all cases _except_ (values.dtype == new_dtype and not copy)
913913
upcast_values = values.astype(new_dtype, copy=copy)
914914

915+
# error: Incompatible return value type (got "Tuple[ndarray[Any, dtype[Any]],
916+
# Union[Union[str, int, float, bool] Union[Period, Timestamp, Timedelta, Any]]]",
917+
# expected "Tuple[NumpyArrayT, Union[Union[str, int, float, bool], Union[Period,
918+
# Timestamp, Timedelta, Any]]]")
915919
return upcast_values, fill_value # type: ignore[return-value]
916920

917921

pandas/core/dtypes/dtypes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -876,15 +876,15 @@ def freq(self):
876876

877877
@classmethod
878878
def _parse_dtype_strict(cls, freq: str_type) -> BaseOffset:
879-
if isinstance(freq, str):
879+
if isinstance(freq, str): # note: freq is already of type str!
880880
if freq.startswith("period[") or freq.startswith("Period["):
881881
m = cls._match.search(freq)
882882
if m is not None:
883883
freq = m.group("freq")
884884

885-
freq = to_offset(freq)
886-
if freq is not None:
887-
return freq
885+
freq_offset = to_offset(freq)
886+
if freq_offset is not None:
887+
return freq_offset
888888

889889
raise ValueError("could not construct PeriodDtype")
890890

0 commit comments

Comments
 (0)