Skip to content

Commit 2e88e7c

Browse files
committed
BUG: reset_index not preserving object dtype for string option (pandas-dev#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 <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> (cherry picked from commit 5e0df27)
1 parent aacdf61 commit 2e88e7c

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v2.1.4.rst

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Bug fixes
3131
- Fixed bug in :meth:`DataFrame.to_hdf` raising when columns have ``StringDtype`` (:issue:`55088`)
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`)
34+
- 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`)
3436
- Fixed bug in :meth:`Series.str.split` and :meth:`Series.str.rsplit` when ``pat=None`` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`56271`)
3537
- Fixed bug in :meth:`Series.str.translate` losing object dtype when string option is set (:issue:`56152`)
3638
-

pandas/core/series.py

+1-1
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

+9
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)