Skip to content

Commit 08c673c

Browse files
GH1264 Version 2.0 cleanup (#1265)
* GH1264 Version 2.0 cleanup * GH1246 Pr feedback
1 parent d5770e2 commit 08c673c

File tree

5 files changed

+106
-6
lines changed

5 files changed

+106
-6
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,16 @@ class Index(IndexOpsMixin[S1]):
346346
def __neg__(self) -> Self: ...
347347
def __nonzero__(self) -> None: ...
348348
__bool__ = ...
349-
def union(self, other: list[HashableT] | Index, sort=...) -> Index: ...
350-
def intersection(self, other: list[S1] | Self, sort: bool = ...) -> Self: ...
351-
def difference(self, other: list | Index, sort: bool | None = None) -> Self: ...
349+
def union(
350+
self, other: list[HashableT] | Self, sort: bool | None = ...
351+
) -> Index: ...
352+
def intersection(self, other: list[S1] | Self, sort: bool | None = ...) -> Self: ...
353+
def difference(self, other: list | Self, sort: bool | None = None) -> Self: ...
352354
def symmetric_difference(
353-
self, other: list[S1] | Self, result_name: Hashable = ..., sort=...
355+
self,
356+
other: list[S1] | Self,
357+
result_name: Hashable = ...,
358+
sort: bool | None = ...,
354359
) -> Self: ...
355360
def get_loc(self, key: Label) -> int | slice | np_ndarray_bool: ...
356361
def get_indexer(
@@ -473,5 +478,6 @@ class Index(IndexOpsMixin[S1]):
473478
| Sequence[float]
474479
),
475480
) -> Self: ...
481+
def infer_objects(self, copy: bool = ...) -> Self: ...
476482

477483
UnknownIndex: TypeAlias = Index[Any]

pandas-stubs/core/indexes/multi.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class MultiIndex(Index):
159159
def equal_levels(self, other): ...
160160
def union(self, other, sort=...): ... # pyrefly: ignore
161161
def intersection( # pyright: ignore[reportIncompatibleMethodOverride]
162-
self, other: list | Self, sort: bool = ...
162+
self, other: list | Self, sort: bool | None = ...
163163
): ...
164164
def difference(self, other, sort=...): ...
165165
def astype(self, dtype: DtypeArg, copy: bool = ...) -> Self: ...

pandas-stubs/core/series.pyi

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1739,9 +1739,18 @@ class Series(IndexOpsMixin[S1], NDFrame):
17391739
*args: Any,
17401740
**kwargs: Any,
17411741
) -> Series[S1]: ...
1742+
@overload
1743+
def cumprod(
1744+
self: Series[_str],
1745+
axis: AxisIndex = ...,
1746+
skipna: _bool = ...,
1747+
*args: Any,
1748+
**kwargs: Any,
1749+
) -> Never: ...
1750+
@overload
17421751
def cumprod(
17431752
self,
1744-
axis: AxisIndex | None = ...,
1753+
axis: AxisIndex = ...,
17451754
skipna: _bool = ...,
17461755
*args: Any,
17471756
**kwargs: Any,
@@ -2219,6 +2228,13 @@ class TimestampSeries(Series[Timestamp]):
22192228
**kwargs: Any,
22202229
) -> Timedelta: ...
22212230
def diff(self, periods: int = ...) -> TimedeltaSeries: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
2231+
def cumprod(
2232+
self,
2233+
axis: AxisIndex = ...,
2234+
skipna: _bool = ...,
2235+
*args: Any,
2236+
**kwargs: Any,
2237+
) -> Never: ...
22222238

22232239
class TimedeltaSeries(Series[Timedelta]):
22242240
# ignores needed because of mypy
@@ -2324,12 +2340,26 @@ class TimedeltaSeries(Series[Timedelta]):
23242340
*args: Any,
23252341
**kwargs: Any,
23262342
) -> TimedeltaSeries: ...
2343+
def cumprod(
2344+
self,
2345+
axis: AxisIndex = ...,
2346+
skipna: _bool = ...,
2347+
*args: Any,
2348+
**kwargs: Any,
2349+
) -> Never: ...
23272350

23282351
class PeriodSeries(Series[Period]):
23292352
@property
23302353
def dt(self) -> PeriodProperties: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
23312354
def __sub__(self, other: PeriodSeries) -> OffsetSeries: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
23322355
def diff(self, periods: int = ...) -> OffsetSeries: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
2356+
def cumprod(
2357+
self,
2358+
axis: AxisIndex = ...,
2359+
skipna: _bool = ...,
2360+
*args: Any,
2361+
**kwargs: Any,
2362+
) -> Never: ...
23332363

23342364
class OffsetSeries(Series[BaseOffset]):
23352365
@overload # type: ignore[override]
@@ -2338,6 +2368,13 @@ class OffsetSeries(Series[BaseOffset]):
23382368
def __radd__( # pyright: ignore[reportIncompatibleMethodOverride]
23392369
self, other: BaseOffset
23402370
) -> OffsetSeries: ...
2371+
def cumprod(
2372+
self,
2373+
axis: AxisIndex = ...,
2374+
skipna: _bool = ...,
2375+
*args: Any,
2376+
**kwargs: Any,
2377+
) -> Never: ...
23412378

23422379
class IntervalSeries(Series[Interval[_OrderableT]], Generic[_OrderableT]):
23432380
@property

tests/test_indexes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ def test_difference_none() -> None:
172172
# GH 253
173173
check(assert_type(ind.difference([1]), "pd.Index[int]"), pd.Index)
174174

175+
# check with sort parameter
176+
check(assert_type(ind.difference([1, None], sort=False), "pd.Index[int]"), pd.Index)
177+
check(assert_type(ind.difference([1], sort=True), "pd.Index[int]"), pd.Index)
178+
175179

176180
def test_str_split() -> None:
177181
# GH 194
@@ -314,6 +318,18 @@ def test_range_index_union():
314318
)
315319

316320

321+
def test_index_union_sort() -> None:
322+
"""Test sort argument in pd.Index.union GH1264."""
323+
check(
324+
assert_type(pd.Index(["e", "f"]).union(["a", "b", "c"], sort=True), pd.Index),
325+
pd.Index,
326+
)
327+
check(
328+
assert_type(pd.Index(["e", "f"]).union(["a", "b", "c"], sort=False), pd.Index),
329+
pd.Index,
330+
)
331+
332+
317333
def test_range_index_start_stop_step():
318334
idx = pd.RangeIndex(3)
319335
check(assert_type(idx.start, int), int)
@@ -1361,3 +1377,10 @@ def test_index_dict() -> None:
13611377
),
13621378
pd.TimedeltaIndex,
13631379
)
1380+
1381+
1382+
def test_index_infer_objects() -> None:
1383+
"""Test infer_objects method on Index."""
1384+
df = pd.DataFrame({"A": ["a", 1, 2, 3]})
1385+
idx = df.set_index("A").index[1:]
1386+
check(assert_type(idx.infer_objects(), pd.Index), pd.Index)

tests/test_series.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3941,3 +3941,37 @@ def test_series_index_type() -> None:
39413941

39423942
if TYPE_CHECKING_INVALID_USAGE:
39433943
t = pd.Series([1, 2], index="ab") # type: ignore[call-overload] # pyright: ignore[reportCallIssue, reportArgumentType]
3944+
3945+
3946+
def test_timedelta_index_cumprod() -> None:
3947+
dates = pd.Series(
3948+
[
3949+
pd.Timestamp("2020-01-01"),
3950+
pd.Timestamp("2020-01-15"),
3951+
pd.Timestamp("2020-02-01"),
3952+
],
3953+
dtype="datetime64[ns]",
3954+
)
3955+
as_period_series = pd.Series(pd.PeriodIndex(dates, freq="M"))
3956+
3957+
offset_series = as_period_series - as_period_series
3958+
3959+
if TYPE_CHECKING_INVALID_USAGE:
3960+
assert_type(pd.Series(["a", "b"]).cumprod(), Never)
3961+
3962+
if TYPE_CHECKING_INVALID_USAGE:
3963+
assert_type(offset_series.cumprod(), Never)
3964+
3965+
if TYPE_CHECKING_INVALID_USAGE:
3966+
assert_type(pd.Series([pd.Timedelta(0), pd.Timedelta(1)]).cumprod(), Never)
3967+
3968+
if TYPE_CHECKING_INVALID_USAGE:
3969+
assert_type(
3970+
pd.Series(
3971+
[pd.Timestamp("2024-04-29"), pd.Timestamp("2034-08-28")]
3972+
).cumprod(),
3973+
Never,
3974+
)
3975+
3976+
if TYPE_CHECKING_INVALID_USAGE:
3977+
assert_type(as_period_series.cumprod(), Never)

0 commit comments

Comments
 (0)