Skip to content

BUG: Index with object dtype and negative loc for insert adding none and replacing existing value #44545

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 3 commits into from
Nov 21, 2021
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ Datetimelike
- Bug in constructing a :class:`Series` from datetime-like strings with mixed timezones incorrectly partially-inferring datetime values (:issue:`40111`)
- Bug in addition with a :class:`Tick` object and a ``np.timedelta64`` object incorrectly raising instead of returning :class:`Timedelta` (:issue:`44474`)
- Bug in adding a ``np.timedelta64`` object to a :class:`BusinessDay` or :class:`CustomBusinessDay` object incorrectly raising (:issue:`44532`)
- 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`)
-

Timedelta
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ def _putmask(self, mask: npt.NDArray[np.bool_], value) -> None:
def insert(self: IntervalArrayT, loc: int, item: Interval) -> IntervalArrayT:
"""
Return a new IntervalArray inserting new item at location. Follows
Python list.append semantics for negative values. Only Interval
Python numpy.insert semantics for negative values. Only Interval
objects and NA can be inserted into an IntervalIndex

Parameters
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6433,7 +6433,7 @@ def insert(self, loc: int, item) -> Index:
"""
Make new Index inserting new item at location.

Follows Python list.append semantics for negative values.
Follows Python numpy.insert semantics for negative values.

Parameters
----------
Expand Down Expand Up @@ -6476,6 +6476,7 @@ def insert(self, loc: int, item) -> Index:

else:
new_values = np.insert(arr, loc, None)
loc = loc if loc >= 0 else loc - 1
new_values[loc] = item

# Use self._constructor instead of Index to retain NumericIndex GH#43921
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/indexes/base_class/test_reshape.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Tests for ndarray-like method on the base Index class
"""
import numpy as np
import pytest

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

@pytest.mark.parametrize(
"val", [(1, 2), np.datetime64("2019-12-31"), np.timedelta64(1, "D")]
)
@pytest.mark.parametrize("loc", [-1, 2])
def test_insert_datetime_into_object(self, loc, val):
# GH#44509
idx = Index(["1", "2", "3"])
result = idx.insert(loc, val)
expected = Index(["1", "2", val, "3"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you check that type(expected[2]) is type(val); numpy often messes that up with dt64/td64

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

numpy/numpy#12550 i dont know exactly what conditions it shows up in, but i get nervous whenever np.datetime64 or np.timedelta64 objects get near an object-dtype ndarray

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for the reference

tm.assert_index_equal(result, expected)
assert type(expected[2]) is type(val)

@pytest.mark.parametrize(
"pos,expected",
[
Expand Down