Skip to content

Commit

Permalink
Backport PR #54884 on branch 2.1.x (REGR: value_counts raises with bi…
Browse files Browse the repository at this point in the history
…ns) (#54932)

Backport PR #54884: REGR: value_counts raises with bins

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl committed Sep 1, 2023
1 parent cf32a23 commit 6fcd7bd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed regression in :func:`read_csv` when ``usecols`` is given and ``dtypes`` is a dict for ``engine="python"`` (:issue:`54868`)
- Fixed regression in :meth:`DataFrame.__setitem__` raising ``AssertionError`` when setting a :class:`Series` with a partial :class:`MultiIndex` (:issue:`54875`)
- Fixed regression in :meth:`Series.value_counts` raising for numeric data if ``bins`` was specified (:issue:`54857`)
- Fixed regression when comparing a :class:`Series` with ``datetime64`` dtype with ``None`` (:issue:`54870`)

.. ---------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,9 @@ def value_counts_internal(
if bins is not None:
from pandas.core.reshape.tile import cut

values = Series(values, copy=False)
if isinstance(values, Series):
values = values._values

try:
ii = cut(values, bins, include_lowest=True)
except TypeError as err:
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,19 @@ def test_value_counts_uint64(self):

tm.assert_series_equal(result, expected)

def test_value_counts_series(self):
# GH#54857
values = np.array([3, 1, 2, 3, 4, np.nan])
result = Series(values).value_counts(bins=3)
expected = Series(
[2, 2, 1],
index=IntervalIndex.from_tuples(
[(0.996, 2.0), (2.0, 3.0), (3.0, 4.0)], dtype="interval[float64, right]"
),
name="count",
)
tm.assert_series_equal(result, expected)


class TestDuplicated:
def test_duplicated_with_nas(self):
Expand Down

0 comments on commit 6fcd7bd

Please sign in to comment.