Skip to content

Fix Series.__new__ #722

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 5 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions pandas-stubs/_libs/interval.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ from pandas._typing import (

VALID_CLOSED: frozenset[str]

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

class _LengthDescriptor:
@overload
Expand Down
48 changes: 21 additions & 27 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,7 @@ NDFrameT = TypeVar("NDFrameT", bound=NDFrame)
IndexT = TypeVar("IndexT", bound=Index)

# Interval closed type
IntervalT = TypeVar(
"IntervalT",
Interval[int],
Interval[float],
Interval[Timestamp],
Interval[Timedelta],
)
IntervalT = TypeVar("IntervalT", bound=Interval)
IntervalClosedType: TypeAlias = Literal["left", "right", "both", "neither"]

TakeIndexer: TypeAlias = Sequence[int] | Sequence[np.integer] | npt.NDArray[np.integer]
Expand Down Expand Up @@ -407,26 +401,26 @@ Function: TypeAlias = np.ufunc | Callable[..., Any]
_HashableTa = TypeVar("_HashableTa", bound=Hashable)
ByT = TypeVar(
"ByT",
str,
bytes,
datetime.date,
datetime.datetime,
datetime.timedelta,
np.datetime64,
np.timedelta64,
bool,
int,
float,
complex,
Timestamp,
Timedelta,
Scalar,
Period,
Interval[int],
Interval[float],
Interval[Timestamp],
Interval[Timedelta],
tuple,
bound=str
| bytes
| datetime.date
| datetime.datetime
| datetime.timedelta
| np.datetime64
| np.timedelta64
| bool
| int
| float
| complex
| Timestamp
| Timedelta
| Scalar
| Period
| Interval[int]
| Interval[float]
| Interval[Timestamp]
| Interval[Timedelta]
| tuple,
)
GroupByObjectNonScalar: TypeAlias = (
tuple
Expand Down
68 changes: 15 additions & 53 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ from matplotlib.axes import (
import numpy as np
from pandas import (
Period,
PeriodDtype,
Timedelta,
Timestamp,
)
Expand Down Expand Up @@ -104,6 +105,7 @@ from pandas._typing import (
CategoryDtypeArg,
ComplexDtypeArg,
CompressionOptions,
Dtype,
DtypeBackend,
DtypeObj,
FilePath,
Expand Down Expand Up @@ -208,92 +210,55 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
_ListLike: TypeAlias = ArrayLike | dict[_str, np.ndarray] | list | tuple | Index
__hash__: ClassVar[None]

# TODO: can __new__ be converted to __init__? Pandas implements __init__
Copy link
Member Author

Choose a reason for hiding this comment

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

Can probably be done if #718 is possible

@overload
def __new__(
cls,
data: DatetimeIndex | Sequence[Timestamp | np.datetime64 | datetime],
index: Axes | None = ...,
dtype=...,
dtype: TimestampDtypeArg = ...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> TimestampSeries: ...
@overload
def __new__(
cls,
data: _ListLike,
dtype: Literal["datetime64[ns]"],
index: Axes | None = ...,
*,
dtype: Literal["datetime64[ns]"],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably should be TimestampDtypeArg

Copy link
Member Author

Choose a reason for hiding this comment

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

I think all the other overloads would also need that: one overload with a specific data and one for _ListLike but a specific dtype?

Copy link
Collaborator

Choose a reason for hiding this comment

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

My concern there would be that it would be too wide. In other words, there would be contents of lists that wouldn't work with certain dtypes. I think the one is in there for TimestampSeries because it handles any kind of list.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably should be TimestampDtypeArg

I think this is the only change left to make.

Copy link
Member Author

Choose a reason for hiding this comment

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

changed

name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> TimestampSeries: ...
@overload
def __new__(
cls,
data: PeriodIndex,
index: Axes | None = ...,
dtype=...,
dtype: PeriodDtype = ...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> PeriodSeries: ...
@overload
def __new__(
cls,
data: TimedeltaIndex | Sequence[Timedelta | np.timedelta64 | timedelta],
index: Axes | None = ...,
dtype=...,
dtype: TimedeltaDtypeArg = ...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> TimedeltaSeries: ...
@overload
def __new__(
cls,
data: IntervalIndex[Interval[int]] | Interval[int] | Sequence[Interval[int]],
index: Axes | None = ...,
dtype=...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> IntervalSeries[int]: ...
@overload
def __new__(
cls,
data: IntervalIndex[Interval[float]]
| Interval[float]
| Sequence[Interval[float]],
index: Axes | None = ...,
dtype=...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> IntervalSeries[float]: ...
@overload
def __new__(
cls,
data: IntervalIndex[Interval[Timestamp]]
| Interval[Timestamp]
| Sequence[Interval[Timestamp]],
index: Axes | None = ...,
dtype=...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> IntervalSeries[Timestamp]: ...
@overload
def __new__(
cls,
data: IntervalIndex[Interval[Timedelta]]
| Interval[Timedelta]
| Sequence[Interval[Timedelta]],
data: IntervalIndex[Interval[_OrderableT]]
Copy link
Member Author

Choose a reason for hiding this comment

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

Needed to switch to bound to simplify these overloads. All the other changes are related to this change.

| Interval[_OrderableT]
| Sequence[Interval[_OrderableT]],
index: Axes | None = ...,
dtype=...,
dtype: Literal["Interval"] = ...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> IntervalSeries[Timedelta]: ...
) -> IntervalSeries[_OrderableT]: ...
@overload
def __new__(
cls,
Expand All @@ -302,27 +267,24 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
index: Axes | None = ...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> Self: ...
@overload
def __new__(
cls,
data: Series[S1] | dict[int, S1] | dict[_str, S1] = ...,
index: Axes | None = ...,
dtype=...,
dtype: Dtype = ...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> Self: ...
@overload
def __new__(
cls,
data: object | _ListLike | None = ...,
index: Axes | None = ...,
dtype=...,
dtype: Dtype = ...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> Series: ...
@property
def hasnans(self) -> bool: ...
Expand Down