From 4ef7fad45c474c126000bbfb22fe7815df764ce1 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Sat, 25 Nov 2023 00:46:45 +0100 Subject: [PATCH 1/3] BUG: reset_index not preserving object dtype for string option --- doc/source/whatsnew/v2.1.4.rst | 2 +- pandas/core/series.py | 2 +- pandas/tests/series/methods/test_reset_index.py | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.1.4.rst b/doc/source/whatsnew/v2.1.4.rst index 543a9864ced26..e57803fcfeea5 100644 --- a/doc/source/whatsnew/v2.1.4.rst +++ b/doc/source/whatsnew/v2.1.4.rst @@ -25,7 +25,7 @@ Bug fixes - Bug in :meth:`Index.__getitem__` returning wrong result for Arrow dtypes and negative stepsize (:issue:`55832`) - Fixed bug in :meth:`DataFrame.__setitem__` casting :class:`Index` with object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`) - 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.reset_index` not preserving object dtype when ``infer_string`` is set (:issue:`56160`) .. --------------------------------------------------------------------------- .. _whatsnew_214.other: diff --git a/pandas/core/series.py b/pandas/core/series.py index a9679f22f9933..f3e05173d9c2a 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1702,7 +1702,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 9e6b4ce0df1d6..56f15c0b0e52c 100644 --- a/pandas/tests/series/methods/test_reset_index.py +++ b/pandas/tests/series/methods/test_reset_index.py @@ -163,6 +163,12 @@ 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 + ser = Series(["a", "b", "c"], dtype=object) + result = ser.reset_index(drop=True) + tm.assert_series_equal(result, ser) + @pytest.mark.parametrize( "array, dtype", From fb8552ca8090ecc7e24608adec25517db9b910ff Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Sun, 26 Nov 2023 12:36:59 +0100 Subject: [PATCH 2/3] Fixup --- pandas/tests/series/methods/test_reset_index.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/tests/series/methods/test_reset_index.py b/pandas/tests/series/methods/test_reset_index.py index 56f15c0b0e52c..9980b4450ba52 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 @@ -165,8 +166,10 @@ def test_reset_index_inplace_and_drop_ignore_name(self): def test_reset_index_drop_infer_string(self): # GH#56160 + pytest.importorskip("pyarrow") ser = Series(["a", "b", "c"], dtype=object) - result = ser.reset_index(drop=True) + with option_context("future.infer_string", True): + result = ser.reset_index(drop=True) tm.assert_series_equal(result, ser) From 6914fcda3417b903eef7bd21259822d973cf1b46 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Dec 2023 04:27:52 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- doc/source/whatsnew/v2.1.4.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.4.rst b/doc/source/whatsnew/v2.1.4.rst index 23246c02b7ac8..1c412701ae5e9 100644 --- a/doc/source/whatsnew/v2.1.4.rst +++ b/doc/source/whatsnew/v2.1.4.rst @@ -29,8 +29,8 @@ Bug fixes - Fixed bug in :meth:`DataFrame.__setitem__` casting :class:`Index` with object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`) - 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.reset_index` not preserving object dtype when ``infer_string`` is set (:issue:`56160`) - 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`)