Skip to content

Backport PR #54884 on branch 2.1.x (REGR: value_counts raises with bins) #54932

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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