Skip to content

Commit 5e0df27

Browse files
phofllithomas1pre-commit-ci[bot]
authored
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>
1 parent c369d93 commit 5e0df27

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v2.1.4.rst

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1719,7 +1719,7 @@ def reset_index(
17191719
return new_ser.__finalize__(self, method="reset_index")
17201720
else:
17211721
return self._constructor(
1722-
self._values.copy(), index=new_index, copy=False
1722+
self._values.copy(), index=new_index, copy=False, dtype=self.dtype
17231723
).__finalize__(self, method="reset_index")
17241724
elif inplace:
17251725
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

@@ -167,6 +168,14 @@ def test_reset_index_inplace_and_drop_ignore_name(self):
167168
expected = Series(range(2), name="old")
168169
tm.assert_series_equal(ser, expected)
169170

171+
def test_reset_index_drop_infer_string(self):
172+
# GH#56160
173+
pytest.importorskip("pyarrow")
174+
ser = Series(["a", "b", "c"], dtype=object)
175+
with option_context("future.infer_string", True):
176+
result = ser.reset_index(drop=True)
177+
tm.assert_series_equal(result, ser)
178+
170179

171180
@pytest.mark.parametrize(
172181
"array, dtype",

0 commit comments

Comments
 (0)