-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
BUG: pd.cut raises error when input is a series that includes NaN and bins is a single number of bins #55684
Comments
This bug doesn't exist on the main development branch: >>> list_input = [1.1, 2.2, 3.3, float('nan')]
>>> series_input = pd.Series(list_input)
>>> pd.cut(list_input, 2)
[(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)
0 (1.098, 2.2]
1 (1.098, 2.2]
2 (2.2, 3.3]
3 NaN
dtype: category
Categories (2, interval[float64, right]): [(1.098, 2.2] < (2.2, 3.3]] It would seem this has been addressed and will be released soon. |
import pandas as pd list_input = [1.1, 2.2, 3.3, float('nan')] Output for List: Output for Series: 0 (1.098, 2.2] |
It seems this was a regression on pandas 2.1, as I get the failure locally with 2.1.1. I didn't yet check whether it's also ready fixed on 2.1.x, though. |
This is fixed on >>> pd.cut(list_input, 2)
[(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)
0 (1.098, 2.2]
1 (1.098, 2.2]
2 (2.2, 3.3]
3 NaN
dtype: category
Categories (2, interval[float64, right]): [(1.098, 2.2] < (2.2, 3.3]] |
I still get an error with the just released 2.1.2. But, it seems this only surfaces when you are not using bottleneck, i.e. in an environment without bottleneck or when it decides to not take that code path. |
|
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
Issue Description
When using
pd.cut
wherex
is a Series that contains NaNs andbins
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
The text was updated successfully, but these errors were encountered: