|
15 | 15 | is_datetime64tz_dtype,
|
16 | 16 | is_datetime_or_timedelta_dtype,
|
17 | 17 | is_integer,
|
| 18 | + is_list_like, |
18 | 19 | is_scalar,
|
19 | 20 | is_timedelta64_dtype,
|
20 | 21 | )
|
@@ -65,11 +66,12 @@ def cut(
|
65 | 66 | ``right == True`` (the default), then the `bins` ``[1, 2, 3, 4]``
|
66 | 67 | indicate (1,2], (2,3], (3,4]. This argument is ignored when
|
67 | 68 | `bins` is an IntervalIndex.
|
68 |
| - labels : array or bool, optional |
| 69 | + labels : array or False, default None |
69 | 70 | Specifies the labels for the returned bins. Must be the same length as
|
70 | 71 | the resulting bins. If False, returns only integer indicators of the
|
71 | 72 | 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. |
73 | 75 | retbins : bool, default False
|
74 | 76 | Whether to return the bins or not. Useful when bins is provided
|
75 | 77 | as a scalar.
|
@@ -286,10 +288,10 @@ def qcut(
|
286 | 288 | q : int or list-like of int
|
287 | 289 | Number of quantiles. 10 for deciles, 4 for quartiles, etc. Alternately
|
288 | 290 | 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 |
290 | 292 | Used as labels for the resulting bins. Must be of the same length as
|
291 | 293 | the resulting bins. If False, return only integer indicators of the
|
292 |
| - bins. |
| 294 | + bins. If True, raises an error. |
293 | 295 | retbins : bool, optional
|
294 | 296 | Whether to return the (bins, labels) or not. Can be useful if bins
|
295 | 297 | is given as a scalar.
|
@@ -395,11 +397,18 @@ def _bins_to_cuts(
|
395 | 397 | labels = _format_labels(
|
396 | 398 | bins, precision, right=right, include_lowest=include_lowest, dtype=dtype
|
397 | 399 | )
|
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): |
399 | 406 | if len(labels) != len(bins) - 1:
|
400 | 407 | raise ValueError(
|
401 | 408 | "Bin labels must be one fewer than the number of bin edges"
|
402 | 409 | )
|
| 410 | + else: |
| 411 | + labels = Categorical(labels, categories=labels, ordered=True) |
403 | 412 | if not is_categorical_dtype(labels):
|
404 | 413 | labels = Categorical(labels, categories=labels, ordered=True)
|
405 | 414 |
|
|
0 commit comments