From f6438b9d73f6006c4e17553e83bec140012825ca Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Thu, 31 Aug 2023 01:05:38 +0200 Subject: [PATCH 1/4] REGR: value_counts raises with bins --- pandas/core/algorithms.py | 1 - pandas/tests/test_algos.py | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 0a9c1aad46f89..377d354213ba9 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -878,7 +878,6 @@ def value_counts_internal( if bins is not None: from pandas.core.reshape.tile import cut - values = Series(values, copy=False) try: ii = cut(values, bins, include_lowest=True) except TypeError as err: diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 856c31b9ccb06..425923b314ac8 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -1412,6 +1412,20 @@ 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]) + with tm.assert_produces_warning(FutureWarning, match="deprecated"): + result = pd.value_counts(values, 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): From 5639b84d40e8b88de039ede69e886f48c83386d1 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Thu, 31 Aug 2023 23:03:33 +0200 Subject: [PATCH 2/4] Update --- pandas/tests/test_algos.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 425923b314ac8..cb703d3439d44 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -1415,8 +1415,7 @@ def test_value_counts_uint64(self): def test_value_counts_series(self): # GH#54857 values = np.array([3, 1, 2, 3, 4, np.nan]) - with tm.assert_produces_warning(FutureWarning, match="deprecated"): - result = pd.value_counts(values, bins=3) + result = Series(values).value_counts(bins=3) expected = Series( [2, 2, 1], index=IntervalIndex.from_tuples( From 98cffef1dced9d17bd192b1ec175901ec002b5fe Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Thu, 31 Aug 2023 23:04:19 +0200 Subject: [PATCH 3/4] Add whastnew --- doc/source/whatsnew/v2.1.1.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.1.1.rst b/doc/source/whatsnew/v2.1.1.rst index f5aa0968ae362..27a45ca423f27 100644 --- a/doc/source/whatsnew/v2.1.1.rst +++ b/doc/source/whatsnew/v2.1.1.rst @@ -14,6 +14,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ - 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`) .. --------------------------------------------------------------------------- .. _whatsnew_211.bug_fixes: From ba3c439bec3a9a953fb51ff54c60e1870b90a835 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Fri, 1 Sep 2023 13:21:22 +0200 Subject: [PATCH 4/4] Update --- pandas/core/algorithms.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 377d354213ba9..5e22b774fb880 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -878,6 +878,9 @@ def value_counts_internal( if bins is not None: from pandas.core.reshape.tile import cut + if isinstance(values, Series): + values = values._values + try: ii = cut(values, bins, include_lowest=True) except TypeError as err: