10000 ERR: Improve error message and doc for invalid labels in cut/qcut by ryankarlos · Pull Request #30691 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

ERR: Improve error message and doc for invalid labels in cut/qcut #30691

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add case for non list, simplify condition block and extra test for cut
  • Loading branch information
ryankarlos committed Jan 7, 2020
commit 174dc3d5abe3b4d4b9087f6a5066511f12cdca05
20 changes: 11 additions & 9 deletions pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,20 +393,22 @@ def _bins_to_cuts(
has_nas = na_mask.any()

if labels is not False:
if labels is None:
if not (labels is None or is_list_like(labels)):
raise ValueError(
"Bin labels must either be False, None or passed in as a "
"list-like argument"
)

elif labels is None:
labels = _format_labels(
bins, precision, right=right, include_lowest=include_lowest, dtype=dtype
)
elif labels is True:

elif len(labels) != len(bins) - 1:
raise ValueError(
"User desired bin labels must be passed in as an argument, "
"not just `True`"
"Bin labels must be one fewer than the number of bin edges"
)
elif is_list_like(labels):
if len(labels) != len(bins) - 1:
raise ValueError(
"Bin labels must be one fewer than the number of bin edges"
)

if not is_categorical_dtype(labels):
labels = Categorical(labels, categories=labels, ordered=True)

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/reshape/test_cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,12 @@ def test_cut_bool_coercion_to_int(bins, box, compare):
expected = cut(data_expected, bins, duplicates="drop")
result = cut(data_result, bins, duplicates="drop")
compare(result, expected)


@pytest.mark.parametrize("labels", ["foo", 1, True])
def test_cut_incorrect_labels(labels):
# GH 13318
values = range(5)
msg = "Bin labels must either be False, None or passed in as a list-like argument"
with pytest.raises(ValueError, match=msg):
cut(values, 4, labels=labels)
7 changes: 4 additions & 3 deletions pandas/tests/reshape/test_qcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,13 @@ def test_qcut_return_intervals():
tm.assert_series_equal(res, exp)


def test_qcut_labels_true():
@pytest.mark.parametrize("labels", ["foo", 1, True])
def test_qcut_incorrect_labels(labels):
# GH 13318
values = range(5)
msg = "User desired bin labels must be passed in as an argument, not just `True`"
msg = "Bin labels must either be False, None or passed in as a list-like argument"
with pytest.raises(ValueError, match=msg):
qcut(values, 4, labels=True)
qcut(values, 4, labels=labels)


@pytest.mark.parametrize("labels", [["a", "b", "c"], list(range(3))])
Expand Down
0