Skip to content

Commit f570a4f

Browse files
committed
BUG: value_counts not preserving object dtype
1 parent 762b61d commit f570a4f

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
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:`Index.insert` casting object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`)
2828
- Fixed bug in :meth:`Series.str.translate` losing object dtype when string option is set (:issue:`56152`)
29+
- Fixed bug in :meth:`Series.value_counts` not preserving object dtype when ``infer_string`` is set (:issue:`56187`)
2930

3031
.. ---------------------------------------------------------------------------
3132
.. _whatsnew_214.other:

pandas/core/algorithms.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,8 @@ def value_counts_internal(
871871
Series,
872872
)
873873

874+
input_dtype = None if not isinstance(values, Series) else values.dtype
875+
874876
index_name = getattr(values, "name", None)
875877
name = "proportion" if normalize else "count"
876878

@@ -929,7 +931,7 @@ def value_counts_internal(
929931

930932
# For backwards compatibility, we let Index do its normal type
931933
# inference, _except_ for if if infers from object to bool.
932-
idx = Index(keys)
934+
idx = Index(keys, dtype=input_dtype)
933935
if idx.dtype == bool and keys.dtype == object:
934936
idx = idx.astype(object)
935937
elif idx.dtype != keys.dtype:

pandas/tests/series/methods/test_value_counts.py

+11
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,14 @@ def test_value_counts_masked(self):
269269
[2, 1, 1], index=Index([2, 1, 3], dtype=dtype), dtype=dtype, name="count"
270270
)
271271
tm.assert_series_equal(result, expected)
272+
273+
def test_value_counts_infer_string(self):
274+
# GH#56187
275+
pytest.importorskip("pyarrow")
276+
277+
ser = Series(["a", "b"], dtype=object)
278+
279+
with pd.option_context("future.infer_string", True):
280+
result = ser.value_counts()
281+
expected = Series([1, 1], index=Index(["a", "b"], dtype=object), name="count")
282+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)