Skip to content

Commit

Permalink
BUG: fixed wrong order of ordered labels in pd.cut()
Browse files Browse the repository at this point in the history
closes pandas-dev#16459

Author: economy <the.economy@gmail.com>

This patch had conflicts when merged, resolved by
Committer: Jeff Reback <jeff.reback@twosigma.com>

Closes pandas-dev#16466 from economy/fix_cut and squashes the following commits:

29128b3 [economy] comments and whatsnew edits
3898b72 [economy] BUG: fixed wrong order of ordered labels in pd.cut()
  • Loading branch information
economy authored and stangirala committed Jun 11, 2017
1 parent 5c573da commit 2cecd75
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 1 addition & 3 deletions doc/source/whatsnew/v0.20.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ Bug Fixes
- Passing an invalid engine to :func:`read_csv` now raises an informative
``ValueError`` rather than ``UnboundLocalError``. (:issue:`16511`)
- Bug in :func:`unique` on an array of tuples (:issue:`16519`)


- Bug in :func:`cut`` when ``labels`` are set, resulting in incorrect label ordering (:issue:`16459`)
- Fixed a compatibility issue with IPython 6.0's tab completion showing deprecation warnings on Categoricals (:issue:`16409`)


Conversion
^^^^^^^^^^

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def _bins_to_cuts(x, bins, right=True, labels=None,
raise ValueError('Bin labels must be one fewer than '
'the number of bin edges')
if not is_categorical_dtype(labels):
labels = Categorical(labels, ordered=True)
labels = Categorical(labels, categories=labels, ordered=True)

np.putmask(ids, na_mask, 0)
result = algos.take_nd(labels, ids - 1)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/reshape/test_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def test_cut_pass_labels(self):

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

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

# issue 16459
labels = ['Good', 'Medium', 'Bad']
result = cut(arr, 3, labels=labels)
exp = cut(arr, 3, labels=Categorical(labels, categories=labels,
ordered=True))
tm.assert_categorical_equal(result, exp)

def test_qcut_include_lowest(self):
values = np.arange(10)

Expand Down

0 comments on commit 2cecd75

Please sign in to comment.