Skip to content

ERR: Improved error message and updated doc in cut/qcut (issue 13318) #16982

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,
labels : array or boolean, default None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about labels: array or False, default None

Used as labels for the resulting bins. Must be of the same length as
the resulting bins. If False, return only integer indicators of the
bins.
bins. If True, raises an error.
retbins : bool, optional
Whether to return the bins or not. Can be useful if bins is given
as a scalar.
Expand Down Expand Up @@ -155,7 +155,7 @@ def qcut(x, q, labels=None, retbins=False, precision=3, duplicates='raise'):
labels : array or boolean, default None
Used as labels for the resulting bins. Must be of the same length as
the resulting bins. If False, return only integer indicators of the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

bins.
bins. If True, raises an error.
retbins : bool, optional
Whether to return the (bins, labels) or not. Can be useful if bins
is given as a scalar.
Expand Down Expand Up @@ -245,7 +245,10 @@ def _bins_to_cuts(x, bins, right=True, labels=None,
has_nas = na_mask.any()

if labels is not False:
if labels is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do something more along the lines

if labels is None:
    .....
elif is_list_like(labels):
    .....
else:
     raise

if labels is True:
raise ValueError("User desired bin labels must be passed "
"in as an argument, not just `True`")
elif labels is None:
labels = _format_labels(bins, precision, right=right,
include_lowest=include_lowest,
dtype=dtype)
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/reshape/test_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ def test_series_retbins(self):
ordered=True)
tm.assert_series_equal(result, expected)

def test_qcut_labels_true(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cycle thru a few more things that are not valid (you can use parametrize) , e.g. 'foo', 1

# issue 13318
values = list(range(10))
with tm.assert_raises_regex(ValueError, "True"):
qcut(values, 5, labels=True)

def test_qcut_duplicates_bin(self):
# GH 7751
values = [0, 0, 0, 0, 1, 2, 3]
Expand Down