diff --git a/doc/source/whatsnew/v2.1.4.rst b/doc/source/whatsnew/v2.1.4.rst index 07197369abc59..9cc79b7090499 100644 --- a/doc/source/whatsnew/v2.1.4.rst +++ b/doc/source/whatsnew/v2.1.4.rst @@ -34,7 +34,6 @@ Bug fixes - Fixed bug in :meth:`Series.reset_index` not preserving object dtype when ``infer_string`` is set (:issue:`56160`) - Fixed bug in :meth:`Series.str.split` and :meth:`Series.str.rsplit` when ``pat=None`` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`56271`) - Fixed bug in :meth:`Series.str.translate` losing object dtype when string option is set (:issue:`56152`) -- Fixed bug in :meth:`Series.value_counts` not preserving object dtype when ``infer_string`` is set (:issue:`56187`) .. --------------------------------------------------------------------------- .. _whatsnew_214.contributors: diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 1c8f876dff408..cfa41a4e1969b 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -871,8 +871,6 @@ def value_counts_internal( Series, ) - input_dtype = None if not isinstance(values, Series) else values.dtype - index_name = getattr(values, "name", None) name = "proportion" if normalize else "count" @@ -931,7 +929,7 @@ def value_counts_internal( # For backwards compatibility, we let Index do its normal type # inference, _except_ for if if infers from object to bool. - idx = Index(keys, dtype=input_dtype if input_dtype != "float16" else None) + idx = Index(keys) if idx.dtype == bool and keys.dtype == object: idx = idx.astype(object) elif ( diff --git a/pandas/tests/series/methods/test_value_counts.py b/pandas/tests/series/methods/test_value_counts.py index 422f0fac37f6d..859010d9c79c6 100644 --- a/pandas/tests/series/methods/test_value_counts.py +++ b/pandas/tests/series/methods/test_value_counts.py @@ -269,14 +269,3 @@ def test_value_counts_masked(self): [2, 1, 1], index=Index([2, 1, 3], dtype=dtype), dtype=dtype, name="count" ) tm.assert_series_equal(result, expected) - - def test_value_counts_infer_string(self): - # GH#56187 - pytest.importorskip("pyarrow") - - ser = Series(["a", "b"], dtype=object) - - with pd.option_context("future.infer_string", True): - result = ser.value_counts() - expected = Series([1, 1], index=Index(["a", "b"], dtype=object), name="count") - tm.assert_series_equal(result, expected)