plydata.cat_tools.cat_inorder¶
-
plydata.cat_tools.
cat_inorder
(c, ordered=None)[source]¶ Reorder categorical by appearance
- Parameters
- clist-like
Values that will make up the categorical.
- orderedbool
If
True
, the categorical is ordered.
- Returns
- out
categorical
Values
- out
Notes
NaN
orNone
are ignored when creating the categories.Examples
>>> import numpy as np >>> x = [4, 1, 3, 4, 4, 7, 3] >>> cat_inorder(x) [4, 1, 3, 4, 4, 7, 3] Categories (4, int64): [4, 1, 3, 7] >>> arr = np.array(x) >>> cat_inorder(arr) [4, 1, 3, 4, 4, 7, 3] Categories (4, int64): [4, 1, 3, 7] >>> c = ['b', 'f', 'c', None, 'c', 'a', 'b', 'e'] >>> cat_inorder(c) ['b', 'f', 'c', NaN, 'c', 'a', 'b', 'e'] Categories (5, object): ['b', 'f', 'c', 'a', 'e'] >>> s = pd.Series(c) >>> cat_inorder(s) ['b', 'f', 'c', NaN, 'c', 'a', 'b', 'e'] Categories (5, object): ['b', 'f', 'c', 'a', 'e'] >>> cat = pd.Categorical(c) >>> cat_inorder(cat) ['b', 'f', 'c', NaN, 'c', 'a', 'b', 'e'] Categories (5, object): ['b', 'f', 'c', 'a', 'e'] >>> cat_inorder(cat, ordered=True) ['b', 'f', 'c', NaN, 'c', 'a', 'b', 'e'] Categories (5, object): ['b' < 'f' < 'c' < 'a' < 'e']
By default, ordered categories remain ordered.
>>> ocat = pd.Categorical(cat, ordered=True) >>> ocat ['b', 'f', 'c', NaN, 'c', 'a', 'b', 'e'] Categories (5, object): ['a' < 'b' < 'c' < 'e' < 'f'] >>> cat_inorder(ocat) ['b', 'f', 'c', NaN, 'c', 'a', 'b', 'e'] Categories (5, object): ['b' < 'f' < 'c' < 'a' < 'e'] >>> cat_inorder(ocat, ordered=False) ['b', 'f', 'c', NaN, 'c', 'a', 'b', 'e'] Categories (5, object): ['b', 'f', 'c', 'a', 'e']