Skip to content

Commit ad4ce7f

Browse files
committed
BUG: Raise error when ordered=True and labels not unique. Updated docstrings (#33141)
1 parent 5882374 commit ad4ce7f

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

pandas/core/reshape/tile.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ def _bins_to_cuts(
383383
):
384384
if not ordered and not labels:
385385
raise ValueError("'labels' must be provided if 'ordered = False'")
386+
if ordered and len(set(labels)) != len(labels):
387+
raise ValueError(
388+
"labels must be unique if ordered=True; pass ordered=False for duplicate labels" # noqa
389+
)
386390

387391
if duplicates not in ["raise", "drop"]:
388392
raise ValueError(
@@ -431,13 +435,11 @@ def _bins_to_cuts(
431435
raise ValueError(
432436
"Bin labels must be one fewer than the number of bin edges"
433437
)
434-
435438
if not is_categorical_dtype(labels):
436-
if len(set(labels)) == len(labels):
437-
labels = Categorical(labels, categories=labels, ordered=ordered)
438-
else:
439-
labels = Categorical(labels, ordered=ordered)
440-
439+
labels = Categorical(
440+
labels, categories=labels if ordered else None, ordered=ordered
441+
)
442+
# TODO: handle mistmach between categorical label order and pandas.cut order.
441443
np.putmask(ids, na_mask, 0)
442444
result = algos.take_nd(labels, ids - 1)
443445

0 commit comments

Comments
 (0)