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 4 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
112 changes: 63 additions & 49 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -211,46 +211,57 @@ 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]
| 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]
| np.timedelta64
| timedelta,
index: Axes | None = ...,
*,
dtype: TimedeltaDtypeArg = ...,
name: Hashable | None = ...,
name: Hashable = ...,
copy: bool = ...,
) -> TimedeltaSeries: ...
@overload
Expand All @@ -260,35 +271,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: Scalar | _ListLike | dict[int, Any] | dict[_str, Any] | None,
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: S1 | _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: Scalar | _ListLike | dict[int, Any] | dict[_str, Any] | None = ...,
index: Axes | None = ...,
*,
dtype: Dtype = ...,
name: Hashable | None = ...,
name: Hashable = ...,
copy: bool = ...,
) -> Series: ...
@property
Expand Down Expand Up @@ -342,8 +357,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 @@ -680,11 +695,13 @@ class Series(IndexOpsMixin[S1], NDFrame):
@overload
def dot(self, other: DataFrame) -> Series[S1]: ...
@overload
def dot(self, other: _ListLike) -> np.ndarray: ...
def dot(
self, other: ArrayLike | dict[_str, np.ndarray] | Sequence[S1] | Index[S1]
) -> 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 +798,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 @@ -834,7 +851,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
axis: AxisIndex = ...,
*args,
**kwargs,
) -> Series[S1]: ...
) -> Self: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make this Series since we really don't know the type of the Series - could be mix of int and float, or one, or the other

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, that made it work!

agg = aggregate
@overload
def transform(
Expand Down Expand Up @@ -902,7 +919,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
inplace: Literal[False] = ...,
level: Level | None = ...,
errors: IgnoreRaise = ...,
) -> Series: ...
) -> Self: ...
@overload
def rename(
self,
Expand All @@ -913,7 +930,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
inplace: Literal[False] = ...,
level: Level | None = ...,
errors: IgnoreRaise = ...,
) -> Series: ...
) -> Self: ...
@overload
def rename(
self,
Expand All @@ -932,7 +949,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
copy: _bool = ...,
limit: int | None = ...,
tolerance: float | None = ...,
) -> Series: ...
) -> Self: ...
@overload
def drop(
self,
Expand All @@ -956,7 +973,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 +1361,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 @@ -1431,14 +1448,10 @@ class Series(IndexOpsMixin[S1], NDFrame):
# just failed to generate these so I couldn't match
# them up.
@overload
def __add__(self, other: TimestampSeries) -> TimestampSeries: ...
@overload
def __add__(self, other: DatetimeIndex) -> TimestampSeries: ...
@overload
def __add__(self, other: Timestamp) -> TimestampSeries: ...
def __add__(self, other: S1 | Self) -> Self: ...
@overload
def __add__(
self, other: num | _str | Timedelta | _ListLike | Series[S1] | np.timedelta64
self, other: num | _str | Timedelta | _ListLike | Series | np.timedelta64
) -> Series: ...
# ignore needed for mypy as we want different results based on the arguments
@overload # type: ignore[override]
Expand Down Expand Up @@ -1479,7 +1492,10 @@ class Series(IndexOpsMixin[S1], NDFrame):
) -> Series[bool]: ...
@overload
def __or__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ...
def __radd__(self, other: num | _str | _ListLike | Series[S1]) -> Series[S1]: ...
@overload
def __radd__(self, other: S1 | Series[S1]) -> Self: ...
@overload
def __radd__(self, other: num | _str | _ListLike | Series) -> Series: ...
# ignore needed for mypy as we want different results based on the arguments
@overload # type: ignore[override]
def __rand__( # type: ignore[misc]
Expand Down Expand Up @@ -1961,11 +1977,16 @@ 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]: ...
def xs(
self,
key: Hashable,
axis: AxisIndex = ...,
level: Level | None = ...,
drop_level: _bool = ...,
) -> Self: ...

class TimestampSeries(Series[Timestamp]):
# ignore needed because of mypy
Expand Down Expand Up @@ -2092,13 +2113,6 @@ class TimedeltaSeries(Series[Timedelta]):
numeric_only: _bool = ...,
**kwargs,
) -> Timedelta: ...
def xs(
self,
key: Hashable,
axis: AxisIndex = ...,
level: Level | None = ...,
drop_level: _bool = ...,
) -> Series: ...

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