Skip to content

Commit 4b31302

Browse files
authored
REGR: value_counts raises with bins (#54884)
1 parent ab34dd6 commit 4b31302

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/source/whatsnew/v2.1.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Fixed regressions
1616
- Fixed regression in :func:`read_csv` when ``usecols`` is given and ``dtypes`` is a dict for ``engine="python"`` (:issue:`54868`)
1717
- Fixed regression in :meth:`.GroupBy.get_group` raising for ``axis=1`` (:issue:`54858`)
1818
- Fixed regression in :meth:`DataFrame.__setitem__` raising ``AssertionError`` when setting a :class:`Series` with a partial :class:`MultiIndex` (:issue:`54875`)
19+
- Fixed regression in :meth:`Series.value_counts` raising for numeric data if ``bins`` was specified (:issue:`54857`)
1920
- Fixed regression when comparing a :class:`Series` with ``datetime64`` dtype with ``None`` (:issue:`54870`)
2021

2122
.. ---------------------------------------------------------------------------

pandas/core/algorithms.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,9 @@ def value_counts_internal(
878878
if bins is not None:
879879
from pandas.core.reshape.tile import cut
880880

881-
values = Series(values, copy=False)
881+
if isinstance(values, Series):
882+
values = values._values
883+
882884
try:
883885
ii = cut(values, bins, include_lowest=True)
884886
except TypeError as err:

pandas/tests/test_algos.py

+13
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,19 @@ def test_value_counts_uint64(self):
14121412

14131413
tm.assert_series_equal(result, expected)
14141414

1415+
def test_value_counts_series(self):
1416+
# GH#54857
1417+
values = np.array([3, 1, 2, 3, 4, np.nan])
1418+
result = Series(values).value_counts(bins=3)
1419+
expected = Series(
1420+
[2, 2, 1],
1421+
index=IntervalIndex.from_tuples(
1422+
[(0.996, 2.0), (2.0, 3.0), (3.0, 4.0)], dtype="interval[float64, right]"
1423+
),
1424+
name="count",
1425+
)
1426+
tm.assert_series_equal(result, expected)
1427+
14151428

14161429
class TestDuplicated:
14171430
def test_duplicated_with_nas(self):

0 commit comments

Comments
 (0)