Skip to content

BUG: pd.cut fails when ordered is set to False and labels is set to a series #36603

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
2 of 3 tasks
Mark-BC opened this issue Sep 24, 2020 · 1 comment · Fixed by #36613
Closed
2 of 3 tasks

BUG: pd.cut fails when ordered is set to False and labels is set to a series #36603

Mark-BC opened this issue Sep 24, 2020 · 1 comment · Fixed by #36613
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@Mark-BC
Copy link

Mark-BC commented Sep 24, 2020

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import pandas as pd

test = pd.DataFrame([['a', 5], ['b', 2], ['c', 6], ['d', 3], ['e', 8]],
                    columns=['row_name', 'row_value'])
cuts = pd.DataFrame([[1, 'odd'], [2, 'even'], [3, 'odd'], [4, 'even'], [5, 'odd'],
                     [6, 'even'], [7, 'odd'], [8, 'even'], [9, 'odd'], [10, '']],
                    columns=['cut_value', 'cut_label'])
print(pd.cut(test.row_value, cuts.cut_value, labels=cuts.cut_label[:-1], ordered=False))

Problem description

When a user wants to call pd.cut with a set of labels that includes duplicate values, they must set ordered to False and set labels to the series of strings to be used.

But running the sample code above, with ordered set to False and labels set to a series, gives this output:

Traceback (most recent call last):
  File "/Users/mark/PycharmProjects/temp/bug1/cut_test.py", line 8, in <module>
    print(pd.cut(test.row_value, cuts.cut_value, labels=cuts.cut_label[:-1], ordered=False))
  File "/Users/mark/PycharmProjects/temp/bug1/venv/lib/python3.7/site-packages/pandas/core/reshape/tile.py", line 284, in cut
    ordered=ordered,
  File "/Users/mark/PycharmProjects/temp/bug1/venv/lib/python3.7/site-packages/pandas/core/reshape/tile.py", line 384, in _bins_to_cuts
    if not ordered and not labels:
  File "/Users/mark/PycharmProjects/temp/bug1/venv/lib/python3.7/site-packages/pandas/core/generic.py", line 1327, in __nonzero__
    f"The truth value of a {type(self).__name__} is ambiguous. "
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Process finished with exit code 1

Expected Output

The expected output is a series of strings taken from the labels list, as documented.

You can see from the Traceback that the error occurs at line 384 of tile.py. which is

    if not ordered and not labels:

If that line is changed to

    if not ordered and labels is None:

then running the sample code again gives the correct output:

0    even
1     odd
2     odd
3    even
4     odd
Name: row_value, dtype: category
Categories (2, object): ['even', 'odd']

Process finished with exit code 0

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2a7d332
python : 3.7.3.final.0
python-bits : 64
OS : Darwin
OS-release : 19.5.0
Version : Darwin Kernel Version 19.5.0: Tue May 26 20:41:44 PDT 2020; root:xnu-6153.121.2~2/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_CA.UTF-8
pandas : 1.1.2
numpy : 1.19.2
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.3
setuptools : 50.3.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@Mark-BC Mark-BC added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 24, 2020
@arw2019
Copy link
Member

arw2019 commented Sep 24, 2020

Thanks for the report! Confirming that this happens on 1.2 master

Output of pd.show_versions()

INSTALLED VERSIONS

commit : ae57e06
python : 3.8.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-48-generic
Version : #52-Ubuntu SMP Thu Sep 10 10:58:49 UTC 2020
machine : x86_64
processor :
byteorder : little
LC_ALL : C.UTF-8
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.0.dev0+487.gae57e062b
numpy : 1.18.5
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 49.1.0.post20200704
Cython : 0.29.21
pytest : 5.4.3
hypothesis : 5.19.0
sphinx : 3.1.1
blosc : None
feather : None
xlsxwriter : 1.2.9
lxml.etree : 4.5.2
html5lib : 1.1
pymysql : None
psycopg2 : 2.8.5 (dt dec pq3 ext lo64)
jinja2 : 2.11.2
IPython : 7.16.1
pandas_datareader: None
bs4 : 4.9.1
bottleneck : 1.3.2
fsspec : 0.7.4
fastparquet : 0.4.0
gcsfs : 0.6.2
matplotlib : 3.2.2
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.4
pandas_gbq : None
pyarrow : 1.0.1
pytables : None
pyxlsb : None
s3fs : 0.4.2
scipy : 1.5.0
sqlalchemy : 1.3.18
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.15.1
xlrd : 1.2.0
xlwt : 1.3.0
numba : 0.50.1

@Mark-BC would you be interested in opening a PR with the fix?

@dsaxton dsaxton removed the Needs Triage Issue that has not been reviewed by a pandas team member label Sep 24, 2020
@dsaxton dsaxton added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Sep 24, 2020
@dsaxton dsaxton added this to the 1.1.3 milestone Sep 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants