Skip to content

Commit 59a1758

Browse files
authored
Adjust tests in root directory for new string option (#56184)
* BUG: mode not preserving object dtype for string option * Adjust tests in root directory for new string option
1 parent 02324e6 commit 59a1758

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
@@ -26,6 +26,7 @@ Bug fixes
2626
- Fixed bug in :meth:`DataFrame.__setitem__` casting :class:`Index` with object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`)
2727
- Fixed bug in :meth:`DataFrame.to_hdf` raising when columns have ``StringDtype`` (:issue:`55088`)
2828
- Fixed bug in :meth:`Index.insert` casting object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`)
29+
- Fixed bug in :meth:`Series.mode` not keeping object dtype when ``infer_string`` is set (:issue:`56183`)
2930
- Fixed bug in :meth:`Series.str.translate` losing object dtype when string option is set (:issue:`56152`)
3031

3132
.. ---------------------------------------------------------------------------

pandas/core/series.py

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

23032303
# Ensure index is type stable (should always use int index)
23042304
return self._constructor(
2305-
res_values, index=range(len(res_values)), name=self.name, copy=False
2305+
res_values,
2306+
index=range(len(res_values)),
2307+
name=self.name,
2308+
copy=False,
2309+
dtype=self.dtype,
23062310
).__finalize__(self, method="mode")
23072311

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

pandas/tests/series/test_reductions.py

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ def test_mode_nullable_dtype(any_numeric_ea_dtype):
5151
tm.assert_series_equal(result, expected)
5252

5353

54+
def test_mode_infer_string():
55+
# GH#56183
56+
pytest.importorskip("pyarrow")
57+
ser = Series(["a", "b"], dtype=object)
58+
with pd.option_context("future.infer_string", True):
59+
result = ser.mode()
60+
expected = Series(["a", "b"], dtype=object)
61+
tm.assert_series_equal(result, expected)
62+
63+
5464
def test_reductions_td64_with_nat():
5565
# GH#8617
5666
ser = Series([0, pd.NaT], dtype="m8[ns]")

pandas/tests/test_algos.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1946,7 +1946,7 @@ def test_timedelta_mode(self):
19461946
tm.assert_series_equal(ser.mode(), exp)
19471947

19481948
def test_mixed_dtype(self):
1949-
exp = Series(["foo"])
1949+
exp = Series(["foo"], dtype=object)
19501950
ser = Series([1, "foo", "foo"])
19511951
tm.assert_numpy_array_equal(algos.mode(ser.values), exp.values)
19521952
tm.assert_series_equal(ser.mode(), exp)

0 commit comments

Comments
 (0)