Skip to content

GH1264 Version 2.0 cleanup #1265

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 2 commits into from
Jun 30, 2025
Merged
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
14 changes: 10 additions & 4 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,16 @@ class Index(IndexOpsMixin[S1]):
def __neg__(self) -> Self: ...
def __nonzero__(self) -> None: ...
__bool__ = ...
def union(self, other: list[HashableT] | Index, sort=...) -> Index: ...
def intersection(self, other: list[S1] | Self, sort: bool = ...) -> Self: ...
def difference(self, other: list | Index, sort: bool | None = None) -> Self: ...
def union(
self, other: list[HashableT] | Self, sort: bool | None = ...
) -> Index: ...
def intersection(self, other: list[S1] | Self, sort: bool | None = ...) -> Self: ...
def difference(self, other: list | Self, sort: bool | None = None) -> Self: ...
def symmetric_difference(
self, other: list[S1] | Self, result_name: Hashable = ..., sort=...
self,
other: list[S1] | Self,
result_name: Hashable = ...,
sort: bool | None = ...,
) -> Self: ...
def get_loc(
self,
Expand Down Expand Up @@ -472,5 +477,6 @@ class Index(IndexOpsMixin[S1]):
| Sequence[float]
),
) -> Self: ...
def infer_objects(self, copy: bool = ...) -> Self: ...

UnknownIndex: TypeAlias = Index[Any]
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/multi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class MultiIndex(Index):
def equal_levels(self, other): ...
def union(self, other, sort=...): ... # pyrefly: ignore
def intersection( # pyright: ignore[reportIncompatibleMethodOverride]
self, other: list | Self, sort: bool = ...
self, other: list | Self, sort: bool | None = ...
): ...
def difference(self, other, sort=...): ...
def astype(self, dtype: DtypeArg, copy: bool = ...) -> Self: ...
Expand Down
39 changes: 38 additions & 1 deletion pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1739,9 +1739,18 @@ class Series(IndexOpsMixin[S1], NDFrame):
*args: Any,
**kwargs: Any,
) -> Series[S1]: ...
@overload
def cumprod(
self: Series[_str],
axis: AxisIndex = ...,
skipna: _bool = ...,
*args: Any,
**kwargs: Any,
) -> Never: ...
@overload
def cumprod(
self,
axis: AxisIndex | None = ...,
axis: AxisIndex = ...,
skipna: _bool = ...,
*args: Any,
**kwargs: Any,
Expand Down Expand Up @@ -2219,6 +2228,13 @@ class TimestampSeries(Series[Timestamp]):
**kwargs: Any,
) -> Timedelta: ...
def diff(self, periods: int = ...) -> TimedeltaSeries: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
def cumprod(
self,
axis: AxisIndex = ...,
skipna: _bool = ...,
*args: Any,
**kwargs: Any,
) -> Never: ...

class TimedeltaSeries(Series[Timedelta]):
# ignores needed because of mypy
Expand Down Expand Up @@ -2324,12 +2340,26 @@ class TimedeltaSeries(Series[Timedelta]):
*args: Any,
**kwargs: Any,
) -> TimedeltaSeries: ...
def cumprod(
self,
axis: AxisIndex = ...,
skipna: _bool = ...,
*args: Any,
**kwargs: Any,
) -> Never: ...

class PeriodSeries(Series[Period]):
@property
def dt(self) -> PeriodProperties: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
def __sub__(self, other: PeriodSeries) -> OffsetSeries: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
def diff(self, periods: int = ...) -> OffsetSeries: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
def cumprod(
self,
axis: AxisIndex = ...,
skipna: _bool = ...,
*args: Any,
**kwargs: Any,
) -> Never: ...

class OffsetSeries(Series[BaseOffset]):
@overload # type: ignore[override]
Expand All @@ -2338,6 +2368,13 @@ class OffsetSeries(Series[BaseOffset]):
def __radd__( # pyright: ignore[reportIncompatibleMethodOverride]
self, other: BaseOffset
) -> OffsetSeries: ...
def cumprod(
self,
axis: AxisIndex = ...,
skipna: _bool = ...,
*args: Any,
**kwargs: Any,
) -> Never: ...

class IntervalSeries(Series[Interval[_OrderableT]], Generic[_OrderableT]):
@property
Expand Down
23 changes: 23 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ def test_difference_none() -> None:
# GH 253
check(assert_type(ind.difference([1]), "pd.Index[int]"), pd.Index)

# check with sort parameter
check(assert_type(ind.difference([1, None], sort=False), "pd.Index[int]"), pd.Index)
check(assert_type(ind.difference([1], sort=True), "pd.Index[int]"), pd.Index)


def test_str_split() -> None:
# GH 194
Expand Down Expand Up @@ -314,6 +318,18 @@ def test_range_index_union():
)


def test_index_union_sort() -> None:
"""Test sort argument in pd.Index.union GH1264."""
check(
assert_type(pd.Index(["e", "f"]).union(["a", "b", "c"], sort=True), pd.Index),
pd.Index,
)
check(
assert_type(pd.Index(["e", "f"]).union(["a", "b", "c"], sort=False), pd.Index),
pd.Index,
)


def test_range_index_start_stop_step():
idx = pd.RangeIndex(3)
check(assert_type(idx.start, int), int)
Expand Down Expand Up @@ -1361,3 +1377,10 @@ def test_index_dict() -> None:
),
pd.TimedeltaIndex,
)


def test_index_infer_objects() -> None:
"""Test infer_objects method on Index."""
df = pd.DataFrame({"A": ["a", 1, 2, 3]})
idx = df.set_index("A").index[1:]
check(assert_type(idx.infer_objects(), pd.Index), pd.Index)
34 changes: 34 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3941,3 +3941,37 @@ def test_series_index_type() -> None:

if TYPE_CHECKING_INVALID_USAGE:
t = pd.Series([1, 2], index="ab") # type: ignore[call-overload] # pyright: ignore[reportCallIssue, reportArgumentType]


def test_timedelta_index_cumprod() -> None:
dates = pd.Series(
[
pd.Timestamp("2020-01-01"),
pd.Timestamp("2020-01-15"),
pd.Timestamp("2020-02-01"),
],
dtype="datetime64[ns]",
)
as_period_series = pd.Series(pd.PeriodIndex(dates, freq="M"))

offset_series = as_period_series - as_period_series

if TYPE_CHECKING_INVALID_USAGE:
assert_type(pd.Series(["a", "b"]).cumprod(), Never)

if TYPE_CHECKING_INVALID_USAGE:
assert_type(offset_series.cumprod(), Never)

if TYPE_CHECKING_INVALID_USAGE:
assert_type(pd.Series([pd.Timedelta(0), pd.Timedelta(1)]).cumprod(), Never)

if TYPE_CHECKING_INVALID_USAGE:
assert_type(
pd.Series(
[pd.Timestamp("2024-04-29"), pd.Timestamp("2034-08-28")]
).cumprod(),
Never,
)

if TYPE_CHECKING_INVALID_USAGE:
assert_type(as_period_series.cumprod(), Never)