Skip to content

Commit 87fec5b

Browse files
committed
allow unordered categorical to order/sort (exclude only min/max)
1 parent 221b493 commit 87fec5b

File tree

4 files changed

+13
-31
lines changed

4 files changed

+13
-31
lines changed

doc/source/categorical.rst

+2-6
Original file line numberDiff line numberDiff line change
@@ -280,16 +280,12 @@ Sorting and Order
280280
The default for construction has change in v0.16.0 to ``ordered=False``, from the prior implicit ``ordered=True``
281281

282282
If categorical data is ordered (``s.cat.ordered == True``), then the order of the categories has a
283-
meaning and certain operations are possible. If the categorical is unordered, a `TypeError` is
284-
raised.
283+
meaning and certain operations are possible. If the categorical is unordered, ``.min()/.max()`` will raise a `TypeError`.
285284

286285
.. ipython:: python
287286
288287
s = Series(Categorical(["a","b","c","a"], ordered=False))
289-
try:
290-
s.sort()
291-
except TypeError as e:
292-
print("TypeError: " + str(e))
288+
s.sort()
293289
s = Series(["a","b","c","a"]).astype('category', ordered=True)
294290
s.sort()
295291
s

doc/source/whatsnew/v0.16.0.txt

-16
Original file line numberDiff line numberDiff line change
@@ -428,22 +428,6 @@ For ease of creation of series of categorical data, we have added the ability to
428428
s = Series(["a","b","c","a"]).astype('category',categories=list('abcdef'),ordered=False)
429429
s
430430

431-
.. warning::
432-
433-
This simple API change may have suprising effects if a user is relying on the previous defaulted behavior implicity. In particular,
434-
sorting operations with a ``Categorical`` will now raise an error:
435-
436-
.. code-block:: python
437-
438-
In [1]: df = DataFrame({ 'A' : Series(list('aabc')).astype('category'), 'B' : np.arange(4) })
439-
440-
In [2]: df['A'].order()
441-
TypeError: Categorical is not ordered for operation argsort
442-
you can use .as_ordered() to change the Categorical to an ordered one
443-
444-
The solution is to make 'A' orderable, e.g. ``df['A'] = df['A'].cat.as_ordered()``
445-
446-
447431
Indexing Changes
448432
~~~~~~~~~~~~~~~~
449433

pandas/core/categorical.py

-2
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,6 @@ def argsort(self, ascending=True, **kwargs):
10131013
-------
10141014
argsorted : numpy array
10151015
"""
1016-
self.check_for_ordered('argsort')
10171016
result = np.argsort(self._codes.copy(), **kwargs)
10181017
if not ascending:
10191018
result = result[::-1]
@@ -1044,7 +1043,6 @@ def order(self, inplace=False, ascending=True, na_position='last'):
10441043
--------
10451044
Category.sort
10461045
"""
1047-
self.check_for_ordered('sort')
10481046
if na_position not in ['last','first']:
10491047
raise ValueError('invalid na_position: {!r}'.format(na_position))
10501048

pandas/tests/test_categorical.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -918,9 +918,11 @@ def test_mode(self):
918918

919919
def test_sort(self):
920920

921-
# unordered cats are not sortable
921+
# unordered cats are sortable
922922
cat = Categorical(["a","b","b","a"], ordered=False)
923-
self.assertRaises(TypeError, lambda : cat.sort())
923+
cat.order()
924+
cat.sort()
925+
924926
cat = Categorical(["a","c","b","d"], ordered=True)
925927

926928
# order
@@ -1767,9 +1769,12 @@ def test_count(self):
17671769

17681770
def test_sort(self):
17691771

1770-
# unordered cats are not sortable
17711772
cat = Series(Categorical(["a","b","b","a"], ordered=False))
1772-
self.assertRaises(TypeError, lambda : cat.sort())
1773+
1774+
# sort in the categories order
1775+
expected = Series(Categorical(["a","a","b","b"], ordered=False),index=[0,3,1,2])
1776+
result = cat.order()
1777+
tm.assert_series_equal(result, expected)
17731778

17741779
cat = Series(Categorical(["a","c","b","d"], ordered=True))
17751780

@@ -1803,9 +1808,8 @@ def test_sort(self):
18031808
self.assertEqual(res["sort"].dtype, "category")
18041809
self.assertEqual(res["unsort"].dtype, "category")
18051810

1806-
def f():
1807-
df.sort(columns=["unsort"], ascending=False)
1808-
self.assertRaises(TypeError, f)
1811+
# unordered cat, but we allow this
1812+
df.sort(columns=["unsort"], ascending=False)
18091813

18101814
# multi-columns sort
18111815
# GH 7848

0 commit comments

Comments
 (0)