Skip to content

Commit f2ba860

Browse files
committed
BUG: Added parameter in the documentation. Pandas cut, add ordered option. (#33141)
1 parent 0dccd26 commit f2ba860

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

pandas/core/reshape/tile.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def cut(
7373
Specifies the labels for the returned bins. Must be the same length as
7474
the resulting bins. If False, returns only integer indicators of the
7575
bins. This affects the type of the output container (see below).
76-
This argument is ignored when `bins` is an IntervalIndex. If True,
77-
raises an error.
76+
This argument is ignored when `bins` is an IntervalIndex.
77+
If True, raises an error. When `ordered=False`, labels must be provided.
7878
retbins : bool, default False
7979
Whether to return the bins or not. Useful when bins is provided
8080
as a scalar.
@@ -86,6 +86,13 @@ def cut(
8686
If bin edges are not unique, raise ValueError or drop non-uniques.
8787
8888
.. versionadded:: 0.23.0
89+
ordered : bool, default True
90+
Whether the labels are ordered or not. Applies to returned types
91+
Categorical and Series (with Categorical dtype). If True,
92+
the resulting categorical will be ordered. If False, the resulting
93+
categorical will be unordered (labels must be provided).
94+
95+
.. versionadded:: 1.1.0
8996
9097
Returns
9198
-------
@@ -146,6 +153,13 @@ def cut(
146153
[bad, good, medium, medium, good, bad]
147154
Categories (3, object): [bad < medium < good]
148155
156+
``ordered=False`` will result in unordered categories when labels are passed:
157+
158+
>>> pd.cut(np.array([1, 7, 5, 4, 6, 3]),
159+
... 3, labels=["B", "C", "A"])
160+
[B, A, C, C, A, B]
161+
Categories (3, object): [B < C < A]
162+
149163
``labels=False`` implies you just want the bins back.
150164
151165
>>> pd.cut([0, 1, 1, 2], bins=4, labels=False)

0 commit comments

Comments
 (0)