votes up 9

labels must be unique if ordered=True; pass ordered=False for duplicate labels

Package:
pandas
github stars 30911
Exception Class:
ValueError

Raise code

)

        elif labels is None:
            labels = _format_labels(
                bins, precision, right=right, include_lowest=include_lowest, dtype=dtype
            )
        elif ordered and len(set(labels)) != len(labels):
            raise ValueError(
                "labels must be unique if ordered=True; pass ordered=False for duplicate labels"  # noqa
            )
        else:
            if len(labels) != len(bins) - 1:
                raise ValueError(
                    "Bin labels must be one fewer than the number of bin edges"
                )
        if n

Ways to fix

votes up 1 votes down

pandas.cut is used to segment and sort data values into bins. This function is also useful for going from a continuous variable to a categorical variable.

If the ordered parameter is set to True then the labels parameter should contain unique values. Otherwise the ordered parameter should be set to false to avoid the error.

Reproducing the error:

pipenv install pandas

import pandas as pd


df=pd.cut(np.array([1, 7, 5, 4, 6, 3]),3,labels=["bad", "medium", "medium",],ordered=True)
print(df)

The error:

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-24-bf1d370129df> in <module>()
      1 import pandas as pd
      2 
----> 3 df=pd.cut(np.array([1, 7, 5, 4, 6, 3]),3,labels=["bad", "medium", "medium",],ordered=True)
      4 print(df)
/usr/local/lib/python3.7/dist-packages/pandas/core/reshape/tile.py in cut(x, bins, right, labels, retbins, precision, include_lowest, duplicates, ordered)
    282         dtype=dtype,
    283         duplicates=duplicates,
--> 284         ordered=ordered,
    285     )
    286 


/usr/local/lib/python3.7/dist-packages/pandas/core/reshape/tile.py in _bins_to_cuts(x, bins, right, labels, precision, include_lowest, dtype, duplicates, ordered)
    428         elif ordered and len(set(labels)) != len(labels):
    429             raise ValueError(
--> 430                 "labels must be unique if ordered=True; pass ordered=False for duplicate labels"  # noqa
    431             )
    432         else:


ValueError: labels must be unique if ordered=True; pass ordered=False for duplicate labels

Fixed version of the code:

import pandas as pd


df=pd.cut(np.array([1, 7, 5, 4, 6, 3]),3,labels=["bad", "medium", "medium",],ordered=False)
print(df)


Output:

['bad', 'medium', 'medium', 'medium', 'medium', 'bad']
Categories (2, object): ['bad', 'medium']

Jul 17, 2021 kellemnegasi answer
kellemnegasi 11.7k

Add a possible fix

Please authorize to post fix