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