Skip to content

Infer dtype of Series in more cases #766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ class DataFrame(NDFrame, OpsMixin):
@overload
def __getitem__( # type: ignore[misc]
self,
key: Series[_bool]
key: Series
| DataFrame
| Index
| np_ndarray_str
Expand Down
4 changes: 1 addition & 3 deletions pandas-stubs/core/indexes/period.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import numpy as np
import pandas as pd
from pandas import Index
from pandas.core.indexes.accessors import PeriodIndexFieldOps
from pandas.core.indexes.datetimelike import (
DatetimeIndexOpsMixin as DatetimeIndexOpsMixin,
)
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin
from pandas.core.indexes.timedeltas import TimedeltaIndex
from typing_extensions import Self

Expand Down
77 changes: 42 additions & 35 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -211,46 +211,51 @@ class _LocIndexerSeries(_LocIndexer, Generic[S1]):
value: S1 | ArrayLike | Series[S1] | None,
) -> None: ...

_ListLike: TypeAlias = (
ArrayLike | dict[_str, np.ndarray] | Sequence[S1] | IndexOpsMixin[S1]
)

class Series(IndexOpsMixin[S1], NDFrame):
_ListLike: TypeAlias = ArrayLike | dict[_str, np.ndarray] | list | tuple | Index
__hash__: ClassVar[None]

# TODO: can __new__ be converted to __init__? Pandas implements __init__
@overload
def __new__(
def __new__( # type: ignore[misc]
cls,
data: DatetimeIndex | Sequence[Timestamp | np.datetime64 | datetime],
data: DatetimeIndex | Sequence[np.datetime64 | datetime],
index: Axes | None = ...,
*,
dtype: TimestampDtypeArg = ...,
name: Hashable | None = ...,
name: Hashable = ...,
copy: bool = ...,
) -> TimestampSeries: ...
@overload
def __new__(
def __new__( # type: ignore[misc]
cls,
data: _ListLike,
index: Axes | None = ...,
*,
dtype: TimestampDtypeArg,
name: Hashable | None = ...,
name: Hashable = ...,
copy: bool = ...,
) -> TimestampSeries: ...
@overload
def __new__(
def __new__( # type: ignore[misc]
cls,
data: PeriodIndex,
index: Axes | None = ...,
*,
dtype: PeriodDtype = ...,
name: Hashable | None = ...,
name: Hashable = ...,
copy: bool = ...,
) -> PeriodSeries: ...
@overload
def __new__(
def __new__( # type: ignore[misc]
cls,
data: TimedeltaIndex | Sequence[Timedelta | np.timedelta64 | timedelta],
data: TimedeltaIndex | Sequence[np.timedelta64 | timedelta],
index: Axes | None = ...,
*,
dtype: TimedeltaDtypeArg = ...,
name: Hashable | None = ...,
name: Hashable = ...,
copy: bool = ...,
) -> TimedeltaSeries: ...
@overload
Expand All @@ -260,35 +265,39 @@ class Series(IndexOpsMixin[S1], NDFrame):
| Interval[_OrderableT]
| Sequence[Interval[_OrderableT]],
index: Axes | None = ...,
*,
dtype: Literal["Interval"] = ...,
name: Hashable | None = ...,
name: Hashable = ...,
copy: bool = ...,
) -> IntervalSeries[_OrderableT]: ...
@overload
def __new__(
cls,
data: object | _ListLike | Series[S1] | dict[int, S1] | dict[_str, S1] | None,
dtype: type[S1],
data: object,
index: Axes | None = ...,
name: Hashable | None = ...,
*,
dtype: type[S1],
name: Hashable = ...,
copy: bool = ...,
) -> Self: ...
@overload
def __new__(
cls,
data: Series[S1] | dict[int, S1] | dict[_str, S1] = ...,
data: _ListLike[S1] | dict[int, S1] | dict[_str, S1],
index: Axes | None = ...,
*,
dtype: Dtype = ...,
name: Hashable | None = ...,
name: Hashable = ...,
copy: bool = ...,
) -> Self: ...
@overload
def __new__(
cls,
data: object | _ListLike | None = ...,
data: object = ...,
index: Axes | None = ...,
*,
dtype: Dtype = ...,
name: Hashable | None = ...,
name: Hashable = ...,
copy: bool = ...,
) -> Series: ...
@property
Expand Down Expand Up @@ -342,8 +351,8 @@ class Series(IndexOpsMixin[S1], NDFrame):
| Series[S1]
| slice
| MaskType
| tuple[S1 | slice, ...],
) -> Series: ...
| tuple[Hashable | slice, ...],
) -> Self: ...
@overload
def __getitem__(self, idx: int | _str) -> S1: ...
def __setitem__(self, key, value) -> None: ...
Expand Down Expand Up @@ -676,15 +685,15 @@ class Series(IndexOpsMixin[S1], NDFrame):
def diff(self, periods: int = ...) -> Series[S1]: ...
def autocorr(self, lag: int = ...) -> float: ...
@overload
def dot(self, other: Series[S1]) -> Scalar: ...
def dot(self, other: Series[S1]) -> Scalar: ... # type: ignore[misc] # pyright: ignore[reportOverlappingOverload]
@overload
def dot(self, other: DataFrame) -> Series[S1]: ...
@overload
def dot(self, other: _ListLike) -> np.ndarray: ...
def __matmul__(self, other): ...
def __rmatmul__(self, other): ...
@overload
def searchsorted(
def searchsorted( # type: ignore[misc]
self,
value: _ListLike,
side: Literal["left", "right"] = ...,
Expand Down Expand Up @@ -781,7 +790,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
ignore_index: _bool = ...,
inplace: Literal[False] = ...,
key: Callable | None = ...,
) -> Series: ...
) -> Self: ...
@overload
def sort_index(
self,
Expand Down Expand Up @@ -902,7 +911,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
inplace: Literal[False] = ...,
level: Level | None = ...,
errors: IgnoreRaise = ...,
) -> Series: ...
) -> Self: ...
@overload
def rename(
self,
Expand All @@ -913,7 +922,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
inplace: Literal[False] = ...,
level: Level | None = ...,
errors: IgnoreRaise = ...,
) -> Series: ...
) -> Self: ...
@overload
def rename(
self,
Expand All @@ -932,7 +941,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
copy: _bool = ...,
limit: int | None = ...,
tolerance: float | None = ...,
) -> Series: ...
) -> Self: ...
@overload
def drop(
self,
Expand All @@ -956,7 +965,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
level: Level | None = ...,
inplace: Literal[False] = ...,
errors: IgnoreRaise = ...,
) -> Series: ...
) -> Self: ...
@overload
def drop(
self,
Expand Down Expand Up @@ -1344,7 +1353,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
na_option: Literal["keep", "top", "bottom"] = ...,
ascending: _bool = ...,
pct: _bool = ...,
) -> Series: ...
) -> Series[float]: ...
def where(
self,
cond: Series[S1]
Expand Down Expand Up @@ -1961,10 +1970,8 @@ class Series(IndexOpsMixin[S1], NDFrame):
axis: AxisIndex | None = ...,
copy: _bool = ...,
inplace: Literal[False] = ...,
) -> Series: ...
def set_axis(
self, labels, *, axis: Axis = ..., copy: _bool = ...
) -> Series[S1]: ...
) -> Self: ...
def set_axis(self, labels, *, axis: Axis = ..., copy: _bool = ...) -> Self: ...
def __iter__(self) -> Iterator[S1]: ...

class TimestampSeries(Series[Timestamp]):
Expand Down Expand Up @@ -2098,7 +2105,7 @@ class TimedeltaSeries(Series[Timedelta]):
axis: AxisIndex = ...,
level: Level | None = ...,
drop_level: _bool = ...,
) -> Series: ...
) -> Self: ...

class PeriodSeries(Series[Period]):
# ignore needed because of mypy
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ numpy = [
]

[tool.poetry.dev-dependencies]
mypy = "1.5.0"
mypy = "1.5.1"
pandas = "2.0.3"
pyarrow = ">=10.0.1"
pytest = ">=7.1.2"
Expand Down
Loading