Skip to content

Commit

Permalink
Backport PR #56160 on branch 2.1.x (BUG: reset_index not preserving o…
Browse files Browse the repository at this point in the history
…bject dtype for string option) (#56387)
  • Loading branch information
phofl committed Dec 7, 2023
1 parent d1764d4 commit 99c2756
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Bug fixes
- Fixed bug in :meth:`Index.insert` casting object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`)
- Fixed bug in :meth:`Series.__ne__` resulting in False for comparison between ``NA`` and string value for ``dtype="string[pyarrow_numpy]"`` (:issue:`56122`)
- Fixed bug in :meth:`Series.mode` not keeping object dtype when ``infer_string`` is set (:issue:`56183`)
- Fixed bug in :meth:`Series.reset_index` not preserving object dtype when ``infer_string`` is set (:issue:`56160`)
- Fixed bug in :meth:`Series.str.split` and :meth:`Series.str.rsplit` when ``pat=None`` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`56271`)
- Fixed bug in :meth:`Series.str.translate` losing object dtype when string option is set (:issue:`56152`)
-
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,7 @@ def reset_index(
return new_ser.__finalize__(self, method="reset_index")
else:
return self._constructor(
self._values.copy(), index=new_index, copy=False
self._values.copy(), index=new_index, copy=False, dtype=self.dtype
).__finalize__(self, method="reset_index")
elif inplace:
raise TypeError(
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/series/methods/test_reset_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
RangeIndex,
Series,
date_range,
option_context,
)
import pandas._testing as tm

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

def test_reset_index_drop_infer_string(self):
# GH#56160
pytest.importorskip("pyarrow")
ser = Series(["a", "b", "c"], dtype=object)
with option_context("future.infer_string", True):
result = ser.reset_index(drop=True)
tm.assert_series_equal(result, ser)


@pytest.mark.parametrize(
"array, dtype",
Expand Down

0 comments on commit 99c2756

Please sign in to comment.