plydata.cat_tools.cat_inseq

plydata.cat_tools.cat_inseq(c, ordered=None)[source]

Reorder categorical by numerical order

Parameters
clist-like

Values that will make up the categorical.

orderedbool

If True, the categorical is ordered.

Returns
outcategorical

Values

Examples

>>> x = pd.Categorical([5, 1, 3, 2, 4])
>>> cat_inseq(x)
[5, 1, 3, 2, 4]
Categories (5, int64): [1, 2, 3, 4, 5]
>>> x = pd.Categorical([5, 1, '3', 2, 4])
>>> cat_inseq(x)
[5, 1, 3, 2, 4]
Categories (5, int64): [1, 2, 3, 4, 5]

Values that cannot be coerced to numerical turn in NaN, and categories cannot be NaN.

>>> x = pd.Categorical([5, 1, 'three', 2, 4])
>>> cat_inseq(x)
[5, 1, NaN, 2, 4]
Categories (4, int64): [1, 2, 4, 5]

Coerces values to numerical

>>> x = [5, 1, '3', 2, 4]
>>> cat_inseq(x, ordered=True)
[5, 1, 3, 2, 4]
Categories (5, int64): [1 < 2 < 3 < 4 < 5]
>>> x = [5, 1, '3', 2, '4.5']
>>> cat_inseq(x)
[5.0, 1.0, 3.0, 2.0, 4.5]
Categories (5, float64): [1.0, 2.0, 3.0, 4.5, 5.0]

Atleast one of the values must be coercible to the integer

>>> x = ['five', 'one', 'three', 'two', 'four']
>>> cat_inseq(x)
Traceback (most recent call last):
    ...
ValueError: Atleast one existing category must be a number.
>>> x = ['five', 'one', '3', 'two', 'four']
>>> cat_inseq(x)
[NaN, NaN, 3, NaN, NaN]
Categories (1, int64): [3]