Skip to content

Commit 3898b72

Browse files
committed
BUG: fixed wrong order of ordered labels in pd.cut()
1 parent ee8346d commit 3898b72

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

doc/source/whatsnew/v0.20.2.txt

+1-5
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,9 @@ Bug Fixes
4444
- Bug in ``DataFrame.update()`` with ``overwrite=False`` and ``NaN values`` (:issue:`15593`)
4545
- Passing an invalid engine to :func:`read_csv` now raises an informative
4646
``ValueError`` rather than ``UnboundLocalError``. (:issue:`16511`)
47-
48-
49-
50-
47+
- Bug in ``pd.cut`` when ``labels`` are set (:issue:`16459`)
5148
- Fixed a compatibility issue with IPython 6.0's tab completion showing deprecation warnings on Categoricals (:issue:`16409`)
5249

53-
5450
Conversion
5551
^^^^^^^^^^
5652

pandas/core/reshape/tile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def _bins_to_cuts(x, bins, right=True, labels=None,
254254
raise ValueError('Bin labels must be one fewer than '
255255
'the number of bin edges')
256256
if not is_categorical_dtype(labels):
257-
labels = Categorical(labels, ordered=True)
257+
labels = Categorical(labels, categories=labels, ordered=True)
258258

259259
np.putmask(ids, na_mask, 0)
260260
result = algos.take_nd(labels, ids - 1)

pandas/tests/reshape/test_tile.py

+7
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ def test_cut_pass_labels(self):
211211

212212
result = cut(arr, bins, labels=labels)
213213
exp = Categorical(['Medium'] + 4 * ['Small'] + ['Medium', 'Large'],
214+
categories=labels,
214215
ordered=True)
215216
tm.assert_categorical_equal(result, exp)
216217

@@ -219,6 +220,12 @@ def test_cut_pass_labels(self):
219220
exp = Categorical.from_codes([1] + 4 * [0] + [1, 2], labels)
220221
tm.assert_categorical_equal(result, exp)
221222

223+
labels = ['Good', 'Medium', 'Bad']
224+
result = cut(arr, 3, labels=labels)
225+
exp = cut(arr, 3, labels=Categorical(labels, categories=labels,
226+
ordered=True))
227+
tm.assert_categorical_equal(result, exp)
228+
222229
def test_qcut_include_lowest(self):
223230
values = np.arange(10)
224231

0 commit comments

Comments
 (0)