From 2e88e7c22d0d71681cef13ac89e706882cae1fb5 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Thu, 7 Dec 2023 21:05:12 +0100 Subject: [PATCH] BUG: reset_index not preserving object dtype for string option (#56160) * BUG: reset_index not preserving object dtype for string option * Fixup * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Thomas Li <47963215+lithomas1@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> (cherry picked from commit 5e0df27fadfeb29e76336b527f4c01cd2f575136) --- doc/source/whatsnew/v2.1.4.rst | 2 ++ pandas/core/series.py | 2 +- pandas/tests/series/methods/test_reset_index.py | 9 +++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.4.rst b/doc/source/whatsnew/v2.1.4.rst index 26fd23d80208c..13df35a1477d0 100644 --- a/doc/source/whatsnew/v2.1.4.rst +++ b/doc/source/whatsnew/v2.1.4.rst @@ -31,6 +31,8 @@ Bug fixes - Fixed bug in :meth:`DataFrame.to_hdf` raising when columns have ``StringDtype`` (:issue:`55088`) - 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`) - diff --git a/pandas/core/series.py b/pandas/core/series.py index 7b22d89bfe22d..3218d15d8782a 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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( diff --git a/pandas/tests/series/methods/test_reset_index.py b/pandas/tests/series/methods/test_reset_index.py index db36221d8f510..272c6663a8dbd 100644 --- a/pandas/tests/series/methods/test_reset_index.py +++ b/pandas/tests/series/methods/test_reset_index.py @@ -11,6 +11,7 @@ RangeIndex, Series, date_range, + option_context, ) import pandas._testing as tm @@ -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",