diff --git a/pandas/core/reshape/tile.py b/pandas/core/reshape/tile.py index d8398023a5083..2fdfe46264e56 100644 --- a/pandas/core/reshape/tile.py +++ b/pandas/core/reshape/tile.py @@ -44,7 +44,7 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3, labels : array or boolean, default None Used as labels for the resulting bins. Must be of the same length as the resulting bins. If False, return only integer indicators of the - bins. + bins. If True, raises an error. retbins : bool, optional Whether to return the bins or not. Can be useful if bins is given as a scalar. @@ -155,7 +155,7 @@ def qcut(x, q, labels=None, retbins=False, precision=3, duplicates='raise'): labels : array or boolean, default None Used as labels for the resulting bins. Must be of the same length as the resulting bins. If False, return only integer indicators of the - bins. + bins. If True, raises an error. retbins : bool, optional Whether to return the (bins, labels) or not. Can be useful if bins is given as a scalar. @@ -245,7 +245,10 @@ def _bins_to_cuts(x, bins, right=True, labels=None, has_nas = na_mask.any() if labels is not False: - if labels is None: + if labels is True: + raise ValueError("User desired bin labels must be passed " + "in as an argument, not just `True`") + elif labels is None: labels = _format_labels(bins, precision, right=right, include_lowest=include_lowest, dtype=dtype) diff --git a/pandas/tests/reshape/test_tile.py b/pandas/tests/reshape/test_tile.py index 2523f8ab9f776..1254fb3c84270 100644 --- a/pandas/tests/reshape/test_tile.py +++ b/pandas/tests/reshape/test_tile.py @@ -326,6 +326,12 @@ def test_series_retbins(self): ordered=True) tm.assert_series_equal(result, expected) + def test_qcut_labels_true(self): + # issue 13318 + values = list(range(10)) + with tm.assert_raises_regex(ValueError, "True"): + qcut(values, 5, labels=True) + def test_qcut_duplicates_bin(self): # GH 7751 values = [0, 0, 0, 0, 1, 2, 3]