Skip to content

Commit 8f1dd29

Browse files
authored
BUG: Index with object dtype and negative loc for insert adding none and replacing existing value (#44545)
1 parent eb24ecc commit 8f1dd29

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ Datetimelike
546546
- Bug in constructing a :class:`Series` from datetime-like strings with mixed timezones incorrectly partially-inferring datetime values (:issue:`40111`)
547547
- Bug in addition with a :class:`Tick` object and a ``np.timedelta64`` object incorrectly raising instead of returning :class:`Timedelta` (:issue:`44474`)
548548
- Bug in adding a ``np.timedelta64`` object to a :class:`BusinessDay` or :class:`CustomBusinessDay` object incorrectly raising (:issue:`44532`)
549+
- Bug in :meth:`Index.insert` for inserting ``np.datetime64``, ``np.timedelta64`` or ``tuple`` into :class:`Index` with ``dtype='object'`` with negative loc addine ``None`` and replacing existing value (:issue:`44509`)
549550
-
550551

551552
Timedelta

pandas/core/arrays/interval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ def _putmask(self, mask: npt.NDArray[np.bool_], value) -> None:
14961496
def insert(self: IntervalArrayT, loc: int, item: Interval) -> IntervalArrayT:
14971497
"""
14981498
Return a new IntervalArray inserting new item at location. Follows
1499-
Python list.append semantics for negative values. Only Interval
1499+
Python numpy.insert semantics for negative values. Only Interval
15001500
objects and NA can be inserted into an IntervalIndex
15011501
15021502
Parameters

pandas/core/indexes/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6433,7 +6433,7 @@ def insert(self, loc: int, item) -> Index:
64336433
"""
64346434
Make new Index inserting new item at location.
64356435
6436-
Follows Python list.append semantics for negative values.
6436+
Follows Python numpy.insert semantics for negative values.
64376437
64386438
Parameters
64396439
----------
@@ -6476,6 +6476,7 @@ def insert(self, loc: int, item) -> Index:
64766476

64776477
else:
64786478
new_values = np.insert(arr, loc, None)
6479+
loc = loc if loc >= 0 else loc - 1
64796480
new_values[loc] = item
64806481

64816482
# Use self._constructor instead of Index to retain NumericIndex GH#43921

pandas/tests/indexes/base_class/test_reshape.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Tests for ndarray-like method on the base Index class
33
"""
4+
import numpy as np
45
import pytest
56

67
from pandas import Index
@@ -42,6 +43,18 @@ def test_insert_missing(self, nulls_fixture):
4243
result = Index(list("abc")).insert(1, nulls_fixture)
4344
tm.assert_index_equal(result, expected)
4445

46+
@pytest.mark.parametrize(
47+
"val", [(1, 2), np.datetime64("2019-12-31"), np.timedelta64(1, "D")]
48+
)
49+
@pytest.mark.parametrize("loc", [-1, 2])
50+
def test_insert_datetime_into_object(self, loc, val):
51+
# GH#44509
52+
idx = Index(["1", "2", "3"])
53+
result = idx.insert(loc, val)
54+
expected = Index(["1", "2", val, "3"])
55+
tm.assert_index_equal(result, expected)
56+
assert type(expected[2]) is type(val)
57+
4558
@pytest.mark.parametrize(
4659
"pos,expected",
4760
[

0 commit comments

Comments
 (0)