Skip to content

Commit ab076c5

Browse files
committed
BUG: mode not preserving object dtype for string option (pandas-dev#56184)
* BUG: mode not preserving object dtype for string option * Adjust tests in root directory for new string option (cherry picked from commit 59a1758)
1 parent 7006d99 commit ab076c5

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

doc/source/whatsnew/v2.1.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Bug fixes
3030
- Fixed bug in :meth:`DataFrame.__setitem__` casting :class:`Index` with object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`)
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`)
33+
- Fixed bug in :meth:`Series.mode` not keeping object dtype when ``infer_string`` is set (:issue:`56183`)
3334
- Fixed bug in :meth:`Series.str.split` and :meth:`Series.str.rsplit` when ``pat=None`` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`56271`)
3435
- Fixed bug in :meth:`Series.str.translate` losing object dtype when string option is set (:issue:`56152`)
3536
-

pandas/core/series.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2218,7 +2218,11 @@ def mode(self, dropna: bool = True) -> Series:
22182218

22192219
# Ensure index is type stable (should always use int index)
22202220
return self._constructor(
2221-
res_values, index=range(len(res_values)), name=self.name, copy=False
2221+
res_values,
2222+
index=range(len(res_values)),
2223+
name=self.name,
2224+
copy=False,
2225+
dtype=self.dtype,
22222226
).__finalize__(self, method="mode")
22232227

22242228
def unique(self) -> ArrayLike: # pylint: disable=useless-parent-delegation

pandas/tests/series/test_reductions.py

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ def test_mode_extension_dtype(as_period):
2929
tm.assert_series_equal(res, ser)
3030

3131

32+
def test_mode_infer_string():
33+
# GH#56183
34+
pytest.importorskip("pyarrow")
35+
ser = Series(["a", "b"], dtype=object)
36+
with pd.option_context("future.infer_string", True):
37+
result = ser.mode()
38+
expected = Series(["a", "b"], dtype=object)
39+
tm.assert_series_equal(result, expected)
40+
41+
3242
def test_reductions_td64_with_nat():
3343
# GH#8617
3444
ser = Series([0, pd.NaT], dtype="m8[ns]")

pandas/tests/test_algos.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2102,7 +2102,7 @@ def test_timedelta_mode(self):
21022102
tm.assert_series_equal(ser.mode(), exp)
21032103

21042104
def test_mixed_dtype(self):
2105-
exp = Series(["foo"])
2105+
exp = Series(["foo"], dtype=object)
21062106
ser = Series([1, "foo", "foo"])
21072107
tm.assert_numpy_array_equal(algos.mode(ser.values), exp.values)
21082108
tm.assert_series_equal(ser.mode(), exp)

0 commit comments

Comments
 (0)