Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust tests in root directory for new string option #56184

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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.mode` not keeping object dtype when ``infer_string`` is set (:issue:`56183`)
- Fixed bug in :meth:`Series.str.translate` losing object dtype when string option is set (:issue:`56152`)

.. ---------------------------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2302,7 +2302,11 @@ def mode(self, dropna: bool = True) -> Series:

# Ensure index is type stable (should always use int index)
return self._constructor(
res_values, index=range(len(res_values)), name=self.name, copy=False
res_values,
index=range(len(res_values)),
name=self.name,
copy=False,
dtype=self.dtype,
).__finalize__(self, method="mode")

def unique(self) -> ArrayLike: # pylint: disable=useless-parent-delegation
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def test_mode_nullable_dtype(any_numeric_ea_dtype):
tm.assert_series_equal(result, expected)


def test_mode_infer_string():
# GH#56183
pytest.importorskip("pyarrow")
ser = Series(["a", "b"], dtype=object)
with pd.option_context("future.infer_string", True):
result = ser.mode()
expected = Series(["a", "b"], dtype=object)
tm.assert_series_equal(result, expected)


def test_reductions_td64_with_nat():
# GH#8617
ser = Series([0, pd.NaT], dtype="m8[ns]")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,7 @@ def test_timedelta_mode(self):
tm.assert_series_equal(ser.mode(), exp)

def test_mixed_dtype(self):
exp = Series(["foo"])
exp = Series(["foo"], dtype=object)
ser = Series([1, "foo", "foo"])
tm.assert_numpy_array_equal(algos.mode(ser.values), exp.values)
tm.assert_series_equal(ser.mode(), exp)
Expand Down