Skip to content

Commit e4ee5b3

Browse files
GH1196 Add typing to pd.Index.inser/delete and derived classes (#1197)
1 parent a6faaf3 commit e4ee5b3

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

pandas-stubs/core/indexes/base.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ class Index(IndexOpsMixin[S1]):
403403
def slice_indexer(self, start=..., end=..., step=...): ...
404404
def get_slice_bound(self, label, side): ...
405405
def slice_locs(self, start=..., end=..., step=...): ...
406-
def delete(self, loc): ...
407-
def insert(self, loc, item): ...
406+
def delete(self, loc) -> Self: ...
407+
def insert(self, loc, item) -> Self: ...
408408
def drop(self, labels, errors: _str = ...) -> Self: ...
409409
@property
410410
def shape(self) -> tuple[int, ...]: ...

pandas-stubs/core/indexes/datetimes.pyi

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ class DatetimeIndex(DatetimeTimedeltaMixin[Timestamp], DatetimeIndexProperties):
8080
def searchsorted(self, value, side: str = ..., sorter=...): ...
8181
@property
8282
def inferred_type(self) -> str: ...
83-
def insert(self, loc, item): ...
8483
def indexer_at_time(self, time, asof: bool = ...): ...
8584
def indexer_between_time(
8685
self, start_time, end_time, include_start: bool = ..., include_end: bool = ...

pandas-stubs/core/indexes/timedeltas.pyi

-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class TimedeltaIndex(DatetimeTimedeltaMixin[Timedelta], TimedeltaIndexProperties
7575
def searchsorted(self, value, side: str = ..., sorter=...): ...
7676
@property
7777
def inferred_type(self) -> str: ...
78-
def insert(self, loc, item): ...
7978
def to_series(self, index=..., name: Hashable = ...) -> TimedeltaSeries: ...
8079
def shift(self, periods: int = ..., freq=...) -> Self: ...
8180

tests/test_indexes.py

+30
Original file line numberDiff line numberDiff line change
@@ -1287,3 +1287,33 @@ def test_datetimeindex_shift() -> None:
12871287
def test_timedeltaindex_shift() -> None:
12881288
ind = pd.date_range("1/1/2021", "1/5/2021") - pd.Timestamp("1/3/2019")
12891289
check(assert_type(ind.shift(1), pd.TimedeltaIndex), pd.TimedeltaIndex)
1290+
1291+
1292+
def test_index_insert() -> None:
1293+
"""Test the return type of Index.insert GH1196."""
1294+
idx = pd.Index([1, 2, 3, 4, 5])
1295+
check(assert_type(idx.insert(2, 3), "pd.Index[int]"), pd.Index, np.integer)
1296+
1297+
ind = pd.date_range("1/1/2021", "1/5/2021") - pd.Timestamp("1/3/2019")
1298+
check(
1299+
assert_type(ind.insert(2, pd.Timedelta("1D")), pd.TimedeltaIndex),
1300+
pd.TimedeltaIndex,
1301+
)
1302+
1303+
dt_ind = pd.date_range("2023-01-01", "2023-02-01")
1304+
check(
1305+
assert_type(dt_ind.insert(2, pd.Timestamp(2024, 3, 5)), pd.DatetimeIndex),
1306+
pd.DatetimeIndex,
1307+
)
1308+
1309+
1310+
def test_index_delete() -> None:
1311+
"""Test the return type of Index.delete GH1196."""
1312+
idx = pd.Index([1, 2, 3, 4, 5])
1313+
check(assert_type(idx.delete(2), "pd.Index[int]"), pd.Index, np.integer)
1314+
1315+
ind = pd.date_range("1/1/2021", "1/5/2021") - pd.Timestamp("1/3/2019")
1316+
check(assert_type(ind.delete(2), pd.TimedeltaIndex), pd.TimedeltaIndex)
1317+
1318+
dt_ind = pd.date_range("2023-01-01", "2023-02-01")
1319+
check(assert_type(dt_ind.delete(2), pd.DatetimeIndex), pd.DatetimeIndex)

0 commit comments

Comments
 (0)