Skip to content

Commit 99c2756

Browse files
authored
Backport PR #56160 on branch 2.1.x (BUG: reset_index not preserving object dtype for string option) (#56387)
1 parent d1764d4 commit 99c2756

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v2.1.4.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Bug fixes
3232
- Fixed bug in :meth:`Index.insert` casting object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`)
3333
- Fixed bug in :meth:`Series.__ne__` resulting in False for comparison between ``NA`` and string value for ``dtype="string[pyarrow_numpy]"`` (:issue:`56122`)
3434
- Fixed bug in :meth:`Series.mode` not keeping object dtype when ``infer_string`` is set (:issue:`56183`)
35+
- Fixed bug in :meth:`Series.reset_index` not preserving object dtype when ``infer_string`` is set (:issue:`56160`)
3536
- Fixed bug in :meth:`Series.str.split` and :meth:`Series.str.rsplit` when ``pat=None`` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`56271`)
3637
- Fixed bug in :meth:`Series.str.translate` losing object dtype when string option is set (:issue:`56152`)
3738
-

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,7 @@ def reset_index(
16521652
return new_ser.__finalize__(self, method="reset_index")
16531653
else:
16541654
return self._constructor(
1655-
self._values.copy(), index=new_index, copy=False
1655+
self._values.copy(), index=new_index, copy=False, dtype=self.dtype
16561656
).__finalize__(self, method="reset_index")
16571657
elif inplace:
16581658
raise TypeError(

pandas/tests/series/methods/test_reset_index.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
RangeIndex,
1212
Series,
1313
date_range,
14+
option_context,
1415
)
1516
import pandas._testing as tm
1617

@@ -155,6 +156,14 @@ def test_reset_index_inplace_and_drop_ignore_name(self):
155156
expected = Series(range(2), name="old")
156157
tm.assert_series_equal(ser, expected)
157158

159+
def test_reset_index_drop_infer_string(self):
160+
# GH#56160
161+
pytest.importorskip("pyarrow")
162+
ser = Series(["a", "b", "c"], dtype=object)
163+
with option_context("future.infer_string", True):
164+
result = ser.reset_index(drop=True)
165+
tm.assert_series_equal(result, ser)
166+
158167

159168
@pytest.mark.parametrize(
160169
"array, dtype",

0 commit comments

Comments
 (0)