Skip to content

Commit 0b1f02b

Browse files
committed
add conditions for error messages for label=true and label is_list_like
1 parent b915b47 commit 0b1f02b

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

pandas/core/reshape/tile.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
is_integer,
1919
is_scalar,
2020
is_timedelta64_dtype,
21+
is_list_like
2122
)
2223
from pandas.core.dtypes.generic import ABCSeries
2324
from pandas.core.dtypes.missing import isna
@@ -397,11 +398,17 @@ def _bins_to_cuts(
397398
labels = _format_labels(
398399
bins, precision, right=right, include_lowest=include_lowest, dtype=dtype
399400
)
400-
else:
401+
elif labels:
402+
raise ValueError(
403+
"User desired bin labels must be passed in as an argument, not just `True`"
404+
)
405+
elif is_list_like(labels):
401406
if len(labels) != len(bins) - 1:
402407
raise ValueError(
403408
"Bin labels must be one fewer than the number of bin edges"
404409
)
410+
else:
411+
labels = Categorical(labels, categories=labels, ordered=True)
405412
if not is_categorical_dtype(labels):
406413
labels = Categorical(labels, categories=labels, ordered=True)
407414

pandas/tests/reshape/test_qcut.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,12 @@ def test_qcut_return_intervals():
130130
tm.assert_series_equal(res, exp)
131131

132132

133-
@pytest.mark.parametrize("values", ['foo', 1, list(range(10))])
134133
def test_qcut_labels_true():
135134
# issue 13318
135+
values = range(5)
136136
msg = "User desired bin labels must be passed in as an argument, not just `True`"
137-
with pytest.raises(ValueError) as w:
138-
qcut(values, 5, labels=True)
139-
140-
assert w[0].message[0] == msg
137+
with pytest.raises(ValueError ,match=msg) as w:
138+
qcut(values, 4, labels=True)
141139

142140

143141
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)