-
class pandas.Categorical(values, categories=None, ordered=False, name=None, fastpath=False)
[source] -
Represents a categorical variable in classic R / S-plus fashion
Categoricals
can only take on only a limited, and usually fixed, number of possible values (categories
). In contrast to statistical categorical variables, aCategorical
might have an order, but numerical operations (additions, divisions, ...) are not possible.All values of the
Categorical
are either incategories
ornp.nan
. Assigning values outside ofcategories
will raise aValueError
. Order is defined by the order of thecategories
, not lexical order of the values.Parameters: values : list-like
The values of the categorical. If categories are given, values not in categories will be replaced with NaN.
categories : Index-like (unique), optional
The unique categories for this categorical. If not given, the categories are assumed to be the unique values of values.
ordered : boolean, (default False)
Whether or not this categorical is treated as a ordered categorical. If not given, the resulting categorical will not be ordered.
Raises: ValueError
If the categories do not validate.
TypeError
If an explicit
ordered=True
is given but nocategories
and thevalues
are not sortable.Examples
>>> from pandas import Categorical >>> Categorical([1, 2, 3, 1, 2, 3]) [1, 2, 3, 1, 2, 3] Categories (3, int64): [1 < 2 < 3]
>>> Categorical(['a', 'b', 'c', 'a', 'b', 'c']) [a, b, c, a, b, c] Categories (3, object): [a < b < c]
>>> a = Categorical(['a','b','c','a','b','c'], ['c', 'b', 'a'], ordered=True) >>> a.min() 'c'
Categorical()
2017-01-12 04:44:28
Please login to continue.