Repository: pandas-dev/pandas
Pull request: REGR: value_counts raises with bins
- [ ] closes #54857 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.


Problem statement:
value_counts: TypeError: bins argument only works with numeric data.
Reproducer:
```python
import pandas as pd
import numpy as np

values = np.array([3, 1, 2, 3, 4, np.nan])
pd.value_counts(values, bins=3)  # <- TypeError: bins argument only works with numeric data.
pd.Series(values).value_counts(bins=3)  # <- TypeError: bins argument only works with numeric data.
```

numpy 1.25.2
pandas 2.1.0rc0

```bash
>>> pd.Series(values).value_counts(bins=3)
Traceback (most recent call last):
  File "Miniconda3\envs\modin\lib\site-packages\pandas\core\algorithms.py", line 882, in value_counts_internal
    ii = cut(values, bins, include_lowest=True)
  File "Miniconda3\envs\modin\lib\site-packages\pandas\core\reshape\tile.py", line 258, in cut
    rng = (nanops.nanmin(x), nanops.nanmax(x))
  File "Miniconda3\envs\modin\lib\site-packages\pandas\core\nanops.py", line 147, in f
    result = alt(values, axis=axis, skipna=skipna, **kwds)
  File "Miniconda3\envs\modin\lib\site-packages\pandas\core\nanops.py", line 404, in new_func
    result = func(values, axis=axis, skipna=skipna, mask=mask, **kwargs)
  File "Miniconda3\envs\modin\lib\site-packages\pandas\core\nanops.py", line 1089, in reduction
    values, mask = _get_values(
  File "Miniconda3\envs\modin\lib\site-packages\pandas\core\nanops.py", line 316, in _get_values
    np.putmask(values, mask, fill_value)
TypeError: putmask: first argument must be an array

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "Miniconda3\envs\modin\lib\site-packages\pandas\core\base.py", line 1010, in value_counts
    return algorithms.value_counts_internal(
  File "Miniconda3\envs\modin\lib\site-packages\pandas\core\algorithms.py", line 884, in value_counts_internal
    raise TypeError("bins argument only works with numeric data.") from err
TypeError: bins argument only works with numeric data.
```

Hints from issue discussion:

Commit message: REGR: value_counts raises with bins

Patch to review:
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):


Code review comments:
diff_hunk: @@ -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)
path: pandas/tests/test_algos.py
text: @user1:
Can we just test the `pd.Series(values).values_counts(bins=3)` version instead to avoid the futurewarning?

@author:
Edit: Sorry that was wrong, wrong environment, yes can do