Skip to content
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

Open
2 of 3 tasks
btw08 opened this issue Oct 25, 2023 · 6 comments
Labels
cut cut, qcut Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@btw08
Copy link

btw08 commented Oct 25, 2023

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

@btw08 btw08 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 25, 2023
@btw08 btw08 changed the title BUG: BUG: pd.cut raises error when input is a series that includes NaN and bins is a single number of bins Oct 25, 2023
@paulreece
Copy link
Contributor

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.

@paulreece paulreece added Closing Candidate May be closeable, needs more eyeballs and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 25, 2023
@Pavanmahaveer7
Copy link

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)
pd.cut(series_input, 2)

Output for List:
[(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]]

Output for Series:

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]]

@jorisvandenbossche jorisvandenbossche added Regression Functionality that used to work in a prior pandas version cut cut, qcut and removed Bug Closing Candidate May be closeable, needs more eyeballs labels Oct 26, 2023
@jorisvandenbossche
Copy link
Member

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.

@jorisvandenbossche jorisvandenbossche added this to the 2.1.2 milestone Oct 26, 2023
@lithomas1 lithomas1 modified the milestones: 2.1.2, 2.1.3 Oct 26, 2023
@paulreece
Copy link
Contributor

This is fixed on 2.1.x as well:

>>> 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]]

@jorisvandenbossche
Copy link
Member

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.

@jorisvandenbossche jorisvandenbossche modified the milestones: 2.1.3, 2.1.4 Nov 13, 2023
@lithomas1 lithomas1 modified the milestones: 2.1.4, 2.2 Dec 8, 2023
@lithomas1 lithomas1 modified the milestones: 2.2, 2.2.1 Jan 20, 2024
@lithomas1 lithomas1 modified the milestones: 2.2.1, 2.2.2 Feb 23, 2024
@lithomas1 lithomas1 modified the milestones: 2.2.2, 2.2.3 Apr 10, 2024
@aniruddhakal
Copy link

df.cut method is producing the NaN's of float types. They check True with np.is_nan(), but neither of df.fillna() or df.replace(float('nan'), replacement_value) can't replace it!

@lithomas1 lithomas1 modified the milestones: 2.2.3, 2.3 Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cut cut, qcut Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

No branches or pull requests

6 participants