Skip to content

Commit 4805582

Browse files
committed
DOC: categorical sort doc updates
1 parent 0b1320d commit 4805582

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

doc/source/categorical.rst

+28-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ The categorical data type is useful in the following cases:
5151
variable to a categorical variable will save some memory, see :ref:`here <categorical.memory>`.
5252
* The lexical order of a variable is not the same as the logical order ("one", "two", "three").
5353
By converting to a categorical and specifying an order on the categories, sorting and
54-
min/max will use the logical order instead of the lexical order.
54+
min/max will use the logical order instead of the lexical order, see :ref:`here <catetgorical.sort>`.
5555
* As a signal to other python libraries that this column should be treated as a categorical
5656
variable (e.g. to use suitable statistical methods or plot types).
5757

@@ -265,9 +265,11 @@ or simply set the categories to a predefined scale, use :func:`Categorical.set_c
265265
intentionally or because it is misspelled or (under Python3) due to a type difference (e.g.,
266266
numpys S1 dtype and python strings). This can result in surprising behaviour!
267267

268-
Ordered or not...
268+
Sorting and Order
269269
-----------------
270270

271+
.. _categorical.sort:
272+
271273
If categorical data is ordered (``s.cat.ordered == True``), then the order of the categories has a
272274
meaning and certain operations are possible. If the categorical is unordered, a `TypeError` is
273275
raised.
@@ -296,9 +298,14 @@ This is even true for strings and numeric data:
296298
s
297299
s.min(), s.max()
298300
301+
302+
Reordering
303+
~~~~~~~~~~
304+
299305
Reordering the categories is possible via the :func:`Categorical.reorder_categories` and
300306
the :func:`Categorical.set_categories` methods. For :func:`Categorical.reorder_categories`, all
301-
old categories must be included in the new categories and no new categories are allowed.
307+
old categories must be included in the new categories and no new categories are allowed. This will
308+
necessarily make the sort order the same as the categories order.
302309

303310
.. ipython:: python
304311
@@ -324,6 +331,24 @@ old categories must be included in the new categories and no new categories are
324331
(e.g.``Series.median()``, which would need to compute the mean between two values if the length
325332
of an array is even) do not work and raise a `TypeError`.
326333

334+
Multi Column Sorting
335+
~~~~~~~~~~~~~~~~~~~~
336+
337+
A categorical dtyped column will partcipate in a multi-column sort in a similar manner to other columns.
338+
The ordering of the categorical is determined by the ``categories`` of that columns.
339+
340+
.. ipython:: python
341+
342+
dfs = DataFrame({'A' : Categorical(list('bbeebbaa'),categories=['e','a','b']),
343+
'B' : [1,2,1,2,2,1,2,1] })
344+
dfs.sort(['A','B'])
345+
346+
Reordering the ``categories``, changes a future sort.
347+
348+
.. ipython:: python
349+
350+
dfs['C'] = dfs['A'].cat.reorder_categories(['a','b','e'])
351+
dfs.sort(['C','B'])
327352
328353
Comparisons
329354
-----------

0 commit comments

Comments
 (0)