'labels' must be provided if 'ordered = False'
Package:
pandas
30911

Exception Class:
ValueError
Raise code
precision: int = 3,
include_lowest: bool = False,
dtype=None,
duplicates: str = "raise",
ordered: bool = True,
):
if not ordered and labels is None:
raise ValueError("'labels' must be provided if 'ordered = False'")
if duplicates not in ["raise", "drop"]:
raise ValueError(
"invalid value for 'duplicates' parameter, valid options are: raise, drop"
)
if isinstance(bins, IntervalIndex):
Links to the raise (1)
https://github.com/pandas-dev/pandas/blob/b3e335254f46a526ee3ce9bb757eac4011d9d1fe/pandas/core/reshape/tile.py#L397Ways to fix
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 False then the labels parameter should be given.
Reproducing the error:
pipenv install pandas
import pandas as pd
df=pd.cut(np.array([1, 7, 5, 4, 6, 3]),3,ordered=False)
print(df)
The error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-99dcd27c63ae> in <module>()
1 import pandas as pd
2
----> 3 df=pd.cut(np.array([1, 7, 5, 4, 6, 3]),3,ordered=False)
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)
383 ):
384 if not ordered and labels is None:
--> 385 raise ValueError("'labels' must be provided if 'ordered = False'")
386
387 if duplicates not in ["raise", "drop"]:
ValueError: 'labels' must be provided if 'ordered = False'
Fixed:
import pandas as pd
df=pd.cut(np.array([1, 7, 5, 4, 6, 3]),3,labels=["bad", "medium", "good"],ordered=False)
print(df)
Output:
['bad', 'good', 'medium', 'medium', 'good', 'bad']
Categories (3, object): ['bad', 'medium', 'good'
Add a possible fix
Please authorize to post fix