Skip to content

Commit 2201408

Browse files
authored
Backport PR #55365 on branch 2.1.x (BUG: Index.insert raising when inserting None into new string dtype) (#55383)
BUG: Index.insert raising when inserting None into new string dtype (#55365) (cherry picked from commit 8664572)
1 parent 850ceb7 commit 2201408

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

doc/source/whatsnew/v2.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixed regressions
2222
Bug fixes
2323
~~~~~~~~~
2424
- Fixed bug in :meth:`DataFrame.idxmin` and :meth:`DataFrame.idxmax` raising for arrow dtypes (:issue:`55368`)
25+
- Fixed bug in :meth:`Index.insert` raising when inserting ``None`` into :class:`Index` with ``dtype="string[pyarrow_numpy]"`` (:issue:`55365`)
2526
-
2627

2728
.. ---------------------------------------------------------------------------

pandas/core/arrays/string_arrow.py

+5
Original file line numberDiff line numberDiff line change
@@ -579,3 +579,8 @@ def _reduce(
579579
)
580580
else:
581581
return super()._reduce(name, skipna=skipna, keepdims=keepdims, **kwargs)
582+
583+
def insert(self, loc: int, item) -> ArrowStringArrayNumpySemantics:
584+
if item is np.nan:
585+
item = libmissing.NA
586+
return super().insert(loc, item) # type: ignore[return-value]

pandas/tests/indexes/base_class/test_reshape.py

+8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ def test_insert_datetime_into_object(self, loc, val):
5454
tm.assert_index_equal(result, expected)
5555
assert type(expected[2]) is type(val)
5656

57+
def test_insert_none_into_string_numpy(self):
58+
# GH#55365
59+
pytest.importorskip("pyarrow")
60+
index = Index(["a", "b", "c"], dtype="string[pyarrow_numpy]")
61+
result = index.insert(-1, None)
62+
expected = Index(["a", "b", None, "c"], dtype="string[pyarrow_numpy]")
63+
tm.assert_index_equal(result, expected)
64+
5765
@pytest.mark.parametrize(
5866
"pos,expected",
5967
[

0 commit comments

Comments
 (0)