Skip to content

GH1196 Add typing to pd.Index.inser/delete and derived classes #1197

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 1 commit into from
Apr 23, 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
4 changes: 2 additions & 2 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ class Index(IndexOpsMixin[S1]):
def slice_indexer(self, start=..., end=..., step=...): ...
def get_slice_bound(self, label, side): ...
def slice_locs(self, start=..., end=..., step=...): ...
def delete(self, loc): ...
def insert(self, loc, item): ...
def delete(self, loc) -> Self: ...
def insert(self, loc, item) -> Self: ...
def drop(self, labels, errors: _str = ...) -> Self: ...
@property
def shape(self) -> tuple[int, ...]: ...
Expand Down
1 change: 0 additions & 1 deletion pandas-stubs/core/indexes/datetimes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class DatetimeIndex(DatetimeTimedeltaMixin[Timestamp], DatetimeIndexProperties):
def searchsorted(self, value, side: str = ..., sorter=...): ...
@property
def inferred_type(self) -> str: ...
def insert(self, loc, item): ...
def indexer_at_time(self, time, asof: bool = ...): ...
def indexer_between_time(
self, start_time, end_time, include_start: bool = ..., include_end: bool = ...
Expand Down
1 change: 0 additions & 1 deletion pandas-stubs/core/indexes/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class TimedeltaIndex(DatetimeTimedeltaMixin[Timedelta], TimedeltaIndexProperties
def searchsorted(self, value, side: str = ..., sorter=...): ...
@property
def inferred_type(self) -> str: ...
def insert(self, loc, item): ...
def to_series(self, index=..., name: Hashable = ...) -> TimedeltaSeries: ...
def shift(self, periods: int = ..., freq=...) -> Self: ...

Expand Down
30 changes: 30 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1287,3 +1287,33 @@ def test_datetimeindex_shift() -> None:
def test_timedeltaindex_shift() -> None:
ind = pd.date_range("1/1/2021", "1/5/2021") - pd.Timestamp("1/3/2019")
check(assert_type(ind.shift(1), pd.TimedeltaIndex), pd.TimedeltaIndex)


def test_index_insert() -> None:
"""Test the return type of Index.insert GH1196."""
idx = pd.Index([1, 2, 3, 4, 5])
check(assert_type(idx.insert(2, 3), "pd.Index[int]"), pd.Index, np.integer)

ind = pd.date_range("1/1/2021", "1/5/2021") - pd.Timestamp("1/3/2019")
check(
assert_type(ind.insert(2, pd.Timedelta("1D")), pd.TimedeltaIndex),
pd.TimedeltaIndex,
)

dt_ind = pd.date_range("2023-01-01", "2023-02-01")
check(
assert_type(dt_ind.insert(2, pd.Timestamp(2024, 3, 5)), pd.DatetimeIndex),
pd.DatetimeIndex,
)


def test_index_delete() -> None:
"""Test the return type of Index.delete GH1196."""
idx = pd.Index([1, 2, 3, 4, 5])
check(assert_type(idx.delete(2), "pd.Index[int]"), pd.Index, np.integer)

ind = pd.date_range("1/1/2021", "1/5/2021") - pd.Timestamp("1/3/2019")
check(assert_type(ind.delete(2), pd.TimedeltaIndex), pd.TimedeltaIndex)

dt_ind = pd.date_range("2023-01-01", "2023-02-01")
check(assert_type(dt_ind.delete(2), pd.DatetimeIndex), pd.DatetimeIndex)