Skip to content

Commit 8664572

Browse files
authored
BUG: Index.insert raising when inserting None into new string dtype (#55365)
1 parent f196319 commit 8664572

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
@@ -27,6 +27,7 @@ Bug fixes
2727
- Fixed bug in :meth:`DataFrame.interpolate` raising incorrect error message (:issue:`55347`)
2828
- Fixed bug in :meth:`DataFrame.resample` not respecting ``closed`` and ``label`` arguments for :class:`~pandas.tseries.offsets.BusinessDay` (:issue:`55282`)
2929
- Fixed bug in :meth:`DataFrame.resample` where bin edges were not correct for :class:`~pandas.tseries.offsets.BusinessDay` (:issue:`55281`)
30+
- Fixed bug in :meth:`Index.insert` raising when inserting ``None`` into :class:`Index` with ``dtype="string[pyarrow_numpy]"`` (:issue:`55365`)
3031
- Silence ``Period[B]`` warnings introduced by :issue:`53446` during normal plotting activity (:issue:`55138`)
3132
-
3233

pandas/core/arrays/string_arrow.py

+5
Original file line numberDiff line numberDiff line change
@@ -613,3 +613,8 @@ def _reduce(
613613
)
614614
else:
615615
return super()._reduce(name, skipna=skipna, keepdims=keepdims, **kwargs)
616+
617+
def insert(self, loc: int, item) -> ArrowStringArrayNumpySemantics:
618+
if item is np.nan:
619+
item = libmissing.NA
620+
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)