Skip to content

Commit 2cb2343

Browse files
committed
update docs and error messages for labels
1 parent c5948d1 commit 2cb2343

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

pandas/core/reshape/tile.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
is_datetime64tz_dtype,
1616
is_datetime_or_timedelta_dtype,
1717
is_integer,
18+
is_list_like,
1819
is_scalar,
1920
is_timedelta64_dtype,
2021
)
@@ -65,11 +66,12 @@ def cut(
6566
``right == True`` (the default), then the `bins` ``[1, 2, 3, 4]``
6667
indicate (1,2], (2,3], (3,4]. This argument is ignored when
6768
`bins` is an IntervalIndex.
68-
labels : array or bool, optional
69+
labels : array or False, default None
6970
Specifies the labels for the returned bins. Must be the same length as
7071
the resulting bins. If False, returns only integer indicators of the
7172
bins. This affects the type of the output container (see below).
72-
This argument is ignored when `bins` is an IntervalIndex.
73+
This argument is ignored when `bins` is an IntervalIndex. If True,
74+
raises an error.
7375
retbins : bool, default False
7476
Whether to return the bins or not. Useful when bins is provided
7577
as a scalar.
@@ -286,10 +288,10 @@ def qcut(
286288
q : int or list-like of int
287289
Number of quantiles. 10 for deciles, 4 for quartiles, etc. Alternately
288290
array of quantiles, e.g. [0, .25, .5, .75, 1.] for quartiles.
289-
labels : array or bool, default None
291+
labels : array or False, default None
290292
Used as labels for the resulting bins. Must be of the same length as
291293
the resulting bins. If False, return only integer indicators of the
292-
bins.
294+
bins. If True, raises an error.
293295
retbins : bool, optional
294296
Whether to return the (bins, labels) or not. Can be useful if bins
295297
is given as a scalar.
@@ -395,11 +397,18 @@ def _bins_to_cuts(
395397
labels = _format_labels(
396398
bins, precision, right=right, include_lowest=include_lowest, dtype=dtype
397399
)
398-
else:
400+
elif labels:
401+
raise ValueError(
402+
"User desired bin labels must be passed in as an argument, "
403+
"not just `True`"
404+
)
405+
elif is_list_like(labels):
399406
if len(labels) != len(bins) - 1:
400407
raise ValueError(
401408
"Bin labels must be one fewer than the number of bin edges"
402409
)
410+
else:
411+
labels = Categorical(labels, categories=labels, ordered=True)
403412
if not is_categorical_dtype(labels):
404413
labels = Categorical(labels, categories=labels, ordered=True)
405414

0 commit comments

Comments
 (0)