Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pandas-stubs/_libs/interval.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ VALID_CLOSED: frozenset[str]

_OrderableScalarT = TypeVar("_OrderableScalarT", bound=int | float)
_OrderableTimesT = TypeVar("_OrderableTimesT", bound=Timestamp | Timedelta)
_OrderableT = TypeVar("_OrderableT", bound=int | float | Timestamp | Timedelta)
_OrderableT = TypeVar(
"_OrderableT", bound=int | float | Timestamp | Timedelta, default=Any
)

@type_check_only
class _LengthDescriptor:
Expand Down
60 changes: 34 additions & 26 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ from pandas.tseries.offsets import (
P = ParamSpec("P")

HashableT = TypeVar("HashableT", bound=Hashable)
HashableT0 = TypeVar("HashableT0", bound=Hashable, default=Hashable)
HashableT1 = TypeVar("HashableT1", bound=Hashable)
HashableT2 = TypeVar("HashableT2", bound=Hashable)
HashableT3 = TypeVar("HashableT3", bound=Hashable)
Expand Down Expand Up @@ -774,7 +775,7 @@ XMLParsers: TypeAlias = Literal["lxml", "etree"]
HTMLFlavors: TypeAlias = Literal["lxml", "html5lib", "bs4"]

# Interval closed type
IntervalT = TypeVar("IntervalT", bound=Interval)
IntervalT = TypeVar("IntervalT", bound=Interval, default=Interval)
IntervalLeftRight: TypeAlias = Literal["left", "right"]
IntervalClosedType: TypeAlias = IntervalLeftRight | Literal["both", "neither"]

Expand Down Expand Up @@ -872,7 +873,7 @@ ExcelWriterMergeCells: TypeAlias = bool | Literal["columns"]

# read_csv: usecols
UsecolsArgType: TypeAlias = (
SequenceNotStr[Hashable] | range | AnyArrayLike | Callable[[HashableT], bool] | None
SequenceNotStr[Hashable] | range | AnyArrayLike | Callable[[Hashable], bool] | None
)

# maintain the sub-type of any hashable sequence
Expand Down Expand Up @@ -918,6 +919,7 @@ PyArrowNotStrDtypeArg: TypeAlias = (
StrLike: TypeAlias = str | np.str_

ScalarT = TypeVar("ScalarT", bound=Scalar)
ScalarT0 = TypeVar("ScalarT0", bound=Scalar, default=Scalar)
# Refine the definitions below in 3.9 to use the specialized type.
np_num: TypeAlias = np.bool | np.integer | np.floating | np.complexfloating
np_ndarray_intp: TypeAlias = npt.NDArray[np.intp]
Expand Down Expand Up @@ -959,7 +961,10 @@ np_1darray_dt: TypeAlias = np_1darray[np.datetime64]
np_1darray_td: TypeAlias = np_1darray[np.timedelta64]
np_2darray: TypeAlias = np.ndarray[tuple[int, int], np.dtype[GenericT]]

NDArrayT = TypeVar("NDArrayT", bound=np.ndarray)
if sys.version_info >= (3, 11):
NDArrayT = TypeVar("NDArrayT", bound=np.ndarray)
else:
NDArrayT = TypeVar("NDArrayT", bound=np.ndarray[Any, Any])

DtypeNp = TypeVar("DtypeNp", bound=np.dtype[np.generic])
KeysArgType: TypeAlias = Any
Expand All @@ -969,7 +974,7 @@ ListLikeExceptSeriesAndStr: TypeAlias = (
)
ListLikeU: TypeAlias = Sequence[Any] | np_1darray | Series | Index
ListLikeHashable: TypeAlias = (
MutableSequence[HashableT] | np_1darray | tuple[HashableT, ...] | range
MutableSequence[HashableT0] | np_1darray | tuple[HashableT0, ...] | range
)

class SupportsDType(Protocol[GenericT_co]):
Expand Down Expand Up @@ -1010,8 +1015,9 @@ SeriesDType: TypeAlias = (
| datetime.datetime # includes pd.Timestamp
| datetime.timedelta # includes pd.Timedelta
)
S0 = TypeVar("S0", bound=SeriesDType, default=Any)
S1 = TypeVar("S1", bound=SeriesDType, default=Any)
# Like S1, but without `default=Any`.
# Like S0 and S1, but without `default=Any`.
S2 = TypeVar("S2", bound=SeriesDType)
S2_contra = TypeVar("S2_contra", bound=SeriesDType, contravariant=True)
S2_NDT_contra = TypeVar(
Expand Down Expand Up @@ -1045,14 +1051,14 @@ IndexingInt: TypeAlias = (
)

# AxesData is used for data for Index
AxesData: TypeAlias = Mapping[S3, Any] | Axes | KeysView[S3]
AxesData: TypeAlias = Mapping[S0, Any] | Axes | KeysView[S0]

# Any plain Python or numpy function
Function: TypeAlias = np.ufunc | Callable[..., Any]
# Use a distinct HashableT in shared types to avoid conflicts with
# shared HashableT and HashableT#. This one can be used if the identical
# type is need in a function that uses GroupByObjectNonScalar
_HashableTa = TypeVar("_HashableTa", bound=Hashable)
_HashableTa = TypeVar("_HashableTa", bound=Hashable, default=Hashable)
if TYPE_CHECKING: # noqa: PYI002
ByT = TypeVar(
"ByT",
Expand All @@ -1070,7 +1076,7 @@ if TYPE_CHECKING: # noqa: PYI002
| Scalar
| Period
| Interval[int | float | Timestamp | Timedelta]
| tuple,
| tuple[Any, ...],
)
# Use a distinct SeriesByT when using groupby with Series of known dtype.
# Essentially, an intersection between Series S1 TypeVar, and ByT TypeVar
Expand All @@ -1088,21 +1094,23 @@ if TYPE_CHECKING: # noqa: PYI002
| Period
| Interval[int | float | Timestamp | Timedelta],
)
GroupByObjectNonScalar: TypeAlias = (
tuple[_HashableTa, ...]
| list[_HashableTa]
| Function
| list[Function]
| list[Series]
| np_ndarray
| list[np_ndarray]
| Mapping[Label, Any]
| list[Mapping[Label, Any]]
| list[Index]
| Grouper
| list[Grouper]
)
GroupByObject: TypeAlias = Scalar | Index | GroupByObjectNonScalar | Series
GroupByObjectNonScalar: TypeAlias = (
tuple[_HashableTa, ...]
| list[_HashableTa]
| Function
| list[Function]
| list[Series]
| np_ndarray
| list[np_ndarray]
| Mapping[Label, Any]
| list[Mapping[Label, Any]]
| list[Index]
| Grouper
| list[Grouper]
)
GroupByObject: TypeAlias = (
Scalar | Index | GroupByObjectNonScalar[_HashableTa] | Series
)

StataDateFormat: TypeAlias = Literal[
"tc",
Expand All @@ -1125,10 +1133,10 @@ StataDateFormat: TypeAlias = Literal[
# `DataFrame.replace` also accepts mappings of these.
ReplaceValue: TypeAlias = (
Scalar
| Pattern
| Pattern[Any]
| NAType
| Sequence[Scalar | Pattern]
| Mapping[HashableT, ScalarT]
| Sequence[Scalar | Pattern[Any]]
| Mapping[HashableT0, ScalarT0]
| Series
| None
)
Expand Down
10 changes: 8 additions & 2 deletions pandas-stubs/core/arrays/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from collections.abc import (
Iterator,
Sequence,
)
import sys
from typing import (
Any,
Literal,
Expand Down Expand Up @@ -55,8 +56,13 @@ class ExtensionArray:
def ndim(self) -> int: ...
@property
def nbytes(self) -> int: ...
@overload
def astype(self, dtype: np.dtype, copy: bool = True) -> np_1darray: ...
if sys.version_info >= (3, 11):
@overload
def astype(self, dtype: np.dtype, copy: bool = True) -> np_1darray: ...
else:
@overload
def astype(self, dtype: np.dtype[Any], copy: bool = True) -> np_1darray: ...

@overload
def astype(self, dtype: ExtensionDtype, copy: bool = True) -> ExtensionArray: ...
@overload
Expand Down
11 changes: 9 additions & 2 deletions pandas-stubs/core/arrays/datetimes.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from datetime import tzinfo as _tzinfo
import sys
from typing import Any

import numpy as np
from pandas.core.arrays.datetimelike import (
Expand All @@ -19,8 +21,13 @@ class DatetimeArray(DatetimeLikeArrayMixin, TimelikeOps, DatelikeOps):
__array_priority__: int = ...
def __init__(self, values, dtype=..., freq=..., copy: bool = ...) -> None: ...
# ignore in dtype() is from the pandas source
@property
def dtype(self) -> np.dtype | DatetimeTZDtype: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
if sys.version_info >= (3, 11):
@property
def dtype(self) -> np.dtype | DatetimeTZDtype: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
else:
@property
def dtype(self) -> np.dtype[Any] | DatetimeTZDtype: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]

@property
def tz(self): ...
@tz.setter
Expand Down
12 changes: 10 additions & 2 deletions pandas-stubs/core/arrays/numpy_.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import sys
from typing import Any

import numpy as np
from numpy.lib.mixins import NDArrayOperatorsMixin
from pandas.core.arrays.base import (
Expand All @@ -8,8 +11,13 @@ from pandas.core.arrays.base import (
from pandas.core.dtypes.dtypes import ExtensionDtype

class PandasDtype(ExtensionDtype):
@property
def numpy_dtype(self) -> np.dtype: ...
if sys.version_info >= (3, 11):
@property
def numpy_dtype(self) -> np.dtype: ...
else:
@property
def numpy_dtype(self) -> np.dtype[Any]: ...

@property
def itemsize(self) -> int: ...

Expand Down
9 changes: 7 additions & 2 deletions pandas-stubs/core/computation/ops.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from typing import Any

import numpy as np
Expand Down Expand Up @@ -75,8 +76,12 @@ class UnaryOp(Op):
func = ...
def __init__(self, op: str, operand) -> None: ...
def __call__(self, env): ...
@property
def return_type(self) -> np.dtype: ...
if sys.version_info >= (3, 11):
@property
def return_type(self) -> np.dtype: ...
else:
@property
def return_type(self) -> np.dtype[Any]: ...

class MathCall(Op):
func = ...
Expand Down
28 changes: 21 additions & 7 deletions pandas-stubs/core/construction.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from collections.abc import Sequence
from typing import overload
import sys
from typing import (
Any,
overload,
)

import numpy as np
from pandas.core.arrays.base import ExtensionArray
Expand Down Expand Up @@ -40,9 +44,19 @@ def array(
dtype: PandasFloatDtypeArg | None = None,
copy: bool = True,
) -> FloatingArray: ...
@overload
def array(
data: Sequence[object],
dtype: str | np.dtype | ExtensionDtype | None = None,
copy: bool = True,
) -> ExtensionArray: ...

if sys.version_info >= (3, 11):
@overload
def array(
data: Sequence[object],
dtype: str | np.dtype | ExtensionDtype | None = None,
copy: bool = True,
) -> ExtensionArray: ...

else:
@overload
def array(
data: Sequence[object],
dtype: str | np.dtype[Any] | ExtensionDtype | None = None,
copy: bool = True,
) -> ExtensionArray: ...
9 changes: 7 additions & 2 deletions pandas-stubs/core/dtypes/dtypes.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime as dt
import sys
from typing import (
Any,
Literal,
Expand Down Expand Up @@ -59,5 +60,9 @@ class PeriodDtype(PandasExtensionDtype):

class IntervalDtype(PandasExtensionDtype):
def __init__(self, subtype: str | npt.DTypeLike | None = ...) -> None: ...
@property
def subtype(self) -> np.dtype | None: ...
if sys.version_info >= (3, 11):
@property
def subtype(self) -> np.dtype | None: ...
else:
@property
def subtype(self) -> np.dtype[Any] | None: ...
Loading
Loading