diff --git a/pandas-stubs/core/indexes/base.pyi b/pandas-stubs/core/indexes/base.pyi index 2cef882ec..9fb20aff8 100644 --- a/pandas-stubs/core/indexes/base.pyi +++ b/pandas-stubs/core/indexes/base.pyi @@ -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, ...]: ... diff --git a/pandas-stubs/core/indexes/datetimes.pyi b/pandas-stubs/core/indexes/datetimes.pyi index c1f596c48..7ed87864c 100644 --- a/pandas-stubs/core/indexes/datetimes.pyi +++ b/pandas-stubs/core/indexes/datetimes.pyi @@ -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 = ... diff --git a/pandas-stubs/core/indexes/timedeltas.pyi b/pandas-stubs/core/indexes/timedeltas.pyi index 59b2c7aa3..4b486e855 100644 --- a/pandas-stubs/core/indexes/timedeltas.pyi +++ b/pandas-stubs/core/indexes/timedeltas.pyi @@ -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: ... diff --git a/tests/test_indexes.py b/tests/test_indexes.py index f276fdba1..1c91add0b 100644 --- a/tests/test_indexes.py +++ b/tests/test_indexes.py @@ -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)