Skip to content

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

Closed
@khdlim

Description

@khdlim

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions