Skip to content

BUG: pd.cut raises error when input is a series that includes NaN and bins is a single number of bins #55684

Open
@btw08

Description

@btw08

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

list_input = [1.1, 2.2, 3.3, float('nan')]
series_input = pd.Series(list_input)

pd.cut(list_input, 2) # works as expected; output as follows:
## [(1.098, 2.2], (1.098, 2.2], (2.2, 3.3], NaN]
## Categories (2, interval[float64, right]): [(1.098, 2.2] < (2.2, 3.3]]

pd.cut(series_input, 2) # raises exception:
## TypeError: putmask: first argument must be an array

Issue Description

When using pd.cut where x is a Series that contains NaNs and bins is an integer (specifying # of bins), a TypeError is raised. It's worth noting that this behavior only occurs for series input (and not, apparently, other array types).

Expected Behavior

Convert all non-NA values to integers; keep NAs as NA.

Installed Versions

INSTALLED VERSIONS

commit : e86ed37
python : 3.10.0.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22621
machine : AMD64
processor : Intel64 Family 6 Model 140 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.1.1
numpy : 1.25.2
pytz : 2022.1
dateutil : 2.8.2
setuptools : 65.1.0
pip : 23.3.1
Cython : None
pytest : 7.1.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.0.3
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.3.0
pandas_datareader : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : 2022.8.2
gcsfs : None
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 12.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.3
sqlalchemy : 1.4.41
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2022.7
qtpy : 2.1.0
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    RegressionFunctionality that used to work in a prior pandas versioncutcut, qcut

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions