Skip to content

BUG: pandas.cut does not give the right answer with nullable integer Series input #31643

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

Closed
khdlim opened this issue Feb 4, 2020 · 3 comments

Comments

@khdlim
Copy link

khdlim commented Feb 4, 2020

Code Sample, a copy-pastable example if possible

import numpy
import pandas

input_array = numpy.array([1, 2, numpy.nan, 4, 5])
print('Input array: %s\n' % input_array)

bins = numpy.array([0.5, 2.5, 4.5, 6.5])
print ('Using bins: %s\n' % bins)

test_series = pandas.Series(input_array).astype('Int64')

print('Test series:\n\n%s\n' % test_series)
print('Results of cut:\n\n%s\n'% pandas.cut(test_series, bins))

Console output:

Input array: [ 1.  2. nan  4.  5.]

Using bins: [0.5 2.5 4.5 6.5]

Test series:

0      1
1      2
2    NaN
3      4
4      5
dtype: Int64

Results of cut:

0    (0.5, 2.5]
1    (0.5, 2.5]
2           NaN
3    (0.5, 2.5]
4    (4.5, 6.5]
dtype: category
Categories (3, interval[float64]): [(0.5, 2.5] < (2.5, 4.5] < (4.5, 6.5]]

Problem description

output[3] should be (2.5, 4.5] and not (0.5, 2.5] as shown.
Tested in pandas 1.0.0 and 0.25.3.

Expected Output

Input array: [ 1.  2. nan  4.  5.]

Using bins: [0.5 2.5 4.5 6.5]

Test series:

0      1
1      2
2    NaN
3      4
4      5
dtype: Int64

Results of cut:

0    (0.5, 2.5]
1    (0.5, 2.5]
2           NaN
3    (2.5, 4.5]
4    (4.5, 6.5]
dtype: category
Categories (3, interval[float64]): [(0.5, 2.5] < (2.5, 4.5] < (4.5, 6.5]]

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.10.final.0
python-bits : 64
OS : Linux
OS-release : 4.4.0-173-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.0
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.6.1
pip : 20.0.2
setuptools : 40.6.3
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.11.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@khdlim khdlim changed the title pandas.cut does not give the right answer with nullable integer Series input BUG: pandas.cut does not give the right answer with nullable integer Series input Feb 4, 2020
@selasley
Copy link
Contributor

selasley commented Feb 4, 2020

This seems to be a byproduct of converting test_series to object type with NAN (#31290)

bins = np.array([0.5, 2.5, 4.5, 6.5])
x = np.array([1, 2, numpy.nan, 4, 5], dtype='object')
bins.searchsorted(x, side='left')
   array([1, 1, 0, 1, 3])

If x is converted to float64 with NAN instead,

x = np.array([1, 2, numpy.nan, 4, 5], dtype='float64')
bins.searchsorted(x, side='left')
  array([1, 1, 4, 2, 3])

and the result of the call to cut() is

0    (0.5, 2.5]
1    (0.5, 2.5]
2           NaN
3    (2.5, 4.5]
4    (4.5, 6.5]
dtype: category
Categories (3, interval[float64]): [(0.5, 2.5] < (2.5, 4.5] < (4.5, 6.5]]

@TomAugspurger
Copy link
Contributor

This may be fixed on master:

Results of cut:

0    (0.5, 2.5]
1    (0.5, 2.5]
2           NaN
3    (2.5, 4.5]
4    (4.5, 6.5]
dtype: category
Categories (3, interval[float64]): [(0.5, 2.5] < (2.5, 4.5] < (4.5, 6.5]]

@khdlim
Copy link
Author

khdlim commented Feb 5, 2020

@selasley Thanks for the link, didn't think to search for pd.cut so it didn't show up when i searched for similar issues. Very informative, thanks!

@TomAugspurger Thanks for the heads up for working on this compatibility issue!

@khdlim khdlim closed this as completed Feb 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants