Skip to content

Commit

Permalink
Revert "BUG: value_counts not preserving object dtype"
Browse files Browse the repository at this point in the history
This reverts commit f570a4f
  • Loading branch information
phofl committed Dec 8, 2023
1 parent 11ca55a commit c53c2b5
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 15 deletions.
1 change: 0 additions & 1 deletion doc/source/whatsnew/v2.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 (
Expand Down
11 changes: 0 additions & 11 deletions pandas/tests/series/methods/test_value_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit c53c2b5

Please sign in to comment.