siuba.dply.forcats

fct_reorder

siuba.dply.forcats.fct_reorder(fct, x, func=<function median>, desc=False) → pandas.core.arrays.categorical.Categorical
siuba.dply.forcats.fct_reorder(__data: siuba.siu.Symbolic, *args, **kwargs)
siuba.dply.forcats.fct_reorder(__data: siuba.siu.Call, *args, **kwargs)

Return copy of fct, with categories reordered according to values in x.

Parameters
  • fct – a pandas.Categorical, or array(-like) used to create one.

  • x – values used to reorder categorical. Must be same length as fct.

  • func – function run over all values within a level of the categorical.

  • desc – whether to sort in descending order.

Examples

>>> fct_reorder(['a', 'a', 'b'], [4, 3, 2])
['a', 'a', 'b']
Categories (2, object): ['b', 'a']
>>> fct_reorder(['a', 'a', 'b'], [4, 3, 2], desc = True)
['a', 'a', 'b']
Categories (2, object): ['a', 'b']
>>> fct_reorder(['x', 'x', 'y'], [4, 0, 2], np.max)
['x', 'x', 'y']
Categories (2, object): ['y', 'x']

fct_recode

siuba.dply.forcats.fct_recode(fct, recat=None, **kwargs) → pandas.core.arrays.categorical.Categorical
siuba.dply.forcats.fct_recode(__data: siuba.siu.Symbolic, *args, **kwargs)
siuba.dply.forcats.fct_recode(__data: siuba.siu.Call, *args, **kwargs)

Return copy of fct with renamed categories.

Parameters
  • fct – a pandas.Categorical, or array(-like) used to create one.

  • **kwargs – arguments of form new_name = old_name.

Examples

>>> cat = ['a', 'b', 'c']
>>> fct_recode(cat, z = 'c')
['a', 'b', 'z']
Categories (3, object): ['a', 'b', 'z']

# >>> fct_recode(cat, x = ‘a’, x = ‘b’) # >>> fct_recode(cat, x = [‘a’, ‘b’])

fct_collapse

siuba.dply.forcats.fct_collapse(fct, recat, group_other=None) → pandas.core.arrays.categorical.Categorical
siuba.dply.forcats.fct_collapse(__data: siuba.siu.Symbolic, *args, **kwargs)
siuba.dply.forcats.fct_collapse(__data: siuba.siu.Call, *args, **kwargs)

Return copy of fct with categories renamed. Optionally group all others.

Parameters
  • fct – a pandas.Categorical, or array(-like) used to create one.

  • recat – dictionary of form {new_cat_name: old_cat_name}. old_cat_name may be a list of existing categories, to be given the same name.

  • group_other – an optional string, specifying what all other categories should be named.

Notes

Resulting levels index is ordered according to the earliest level replaced. If we rename the first and last levels to “c”, then “c” is the first level.

Examples

>>> fct_collapse(['a', 'b', 'c'], {'x': 'a'})
['x', 'b', 'c']
Categories (3, object): ['x', 'b', 'c']
>>> fct_collapse(['a', 'b', 'c'], {'x': 'a'}, group_other = 'others')
['x', 'others', 'others']
Categories (2, object): ['x', 'others']
>>> fct_collapse(['a', 'b', 'c'], {'ab': ['a', 'b']})
['ab', 'ab', 'c']
Categories (2, object): ['ab', 'c']
>>> fct_collapse(['a', 'b', None], {'a': ['b']})
['a', 'a', NaN]
Categories (1, object): ['a']

fct_lump

siuba.dply.forcats.fct_lump(fct, n=None, prop=None, w=None, other_level='Other', ties=None) → pandas.core.arrays.categorical.Categorical
siuba.dply.forcats.fct_lump(__data: siuba.siu.Symbolic, *args, **kwargs)
siuba.dply.forcats.fct_lump(__data: siuba.siu.Call, *args, **kwargs)
Parameters
  • fct – a pandas.Categorical, or array(-like) used to create one.

  • n – number of categories to keep.

  • prop – (not implemented) keep categories that occur prop proportion of the time.

  • w – array of weights corresponding to each value in fct.

  • other_level – name for all lumped together levels.

  • ties – (not implemented) method to use in the case of ties.

Notes

Currently, one of n and prop must be specified.

Examples

>>> fct_lump(['a', 'a', 'b', 'c'], n = 1)
['a', 'a', 'Other', 'Other']
Categories (2, object): ['a', 'Other']

# TODO: implement prop arg >>> fct_lump([‘a’, ‘a’, ‘b’, ‘b’, ‘c’, ‘d’], prop = .2) [‘a’, ‘a’, ‘b’, ‘b’, ‘Other’, ‘Other’] Categories (3, object): [‘a’, ‘b’, ‘Other’]

fct_rev

siuba.dply.forcats.fct_rev(fct) → pandas.core.arrays.categorical.Categorical
siuba.dply.forcats.fct_rev(__data: siuba.siu.Symbolic, *args, **kwargs)
siuba.dply.forcats.fct_rev(__data: siuba.siu.Call, *args, **kwargs)

Return a copy of fct with category level order reversed.next

Parameters

fct – a pandas.Categorical, or array(-like) used to create one.