Skip to content

Commit c6123ad

Browse files
Backport PR #36613: BUG: Fix unordered cut with Series labels (#36633)
Co-authored-by: Daniel Saxton <[email protected]>
1 parent 8a92388 commit c6123ad

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v1.1.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Bug fixes
5252
- Bug in :meth:`DataFrame.stack` raising a ``ValueError`` when stacking :class:`MultiIndex` columns based on position when the levels had duplicate names (:issue:`36353`)
5353
- Bug in :meth:`Series.astype` showing too much precision when casting from ``np.float32`` to string dtype (:issue:`36451`)
5454
- Bug in :meth:`Series.isin` and :meth:`DataFrame.isin` when using ``NaN`` and a row length above 1,000,000 (:issue:`22205`)
55+
- Bug in :func:`cut` raising a ``ValueError`` when passed a :class:`Series` of labels with ``ordered=False`` (:issue:`36603`)
5556

5657
.. ---------------------------------------------------------------------------
5758

pandas/core/reshape/tile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ def _bins_to_cuts(
381381
duplicates: str = "raise",
382382
ordered: bool = True,
383383
):
384-
if not ordered and not labels:
384+
if not ordered and labels is None:
385385
raise ValueError("'labels' must be provided if 'ordered = False'")
386386

387387
if duplicates not in ["raise", "drop"]:

pandas/tests/reshape/test_cut.py

+10
Original file line numberDiff line numberDiff line change
@@ -664,3 +664,13 @@ def test_cut_unordered_with_missing_labels_raises_error():
664664
msg = "'labels' must be provided if 'ordered = False'"
665665
with pytest.raises(ValueError, match=msg):
666666
cut([0.5, 3], bins=[0, 1, 2], ordered=False)
667+
668+
669+
def test_cut_unordered_with_series_labels():
670+
# https://github.com/pandas-dev/pandas/issues/36603
671+
s = pd.Series([1, 2, 3, 4, 5])
672+
bins = pd.Series([0, 2, 4, 6])
673+
labels = pd.Series(["a", "b", "c"])
674+
result = pd.cut(s, bins=bins, labels=labels, ordered=False)
675+
expected = pd.Series(["a", "a", "b", "b", "c"], dtype="category")
676+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)