pandas.Categorical
-
class pandas.Categorical(values, categories=None, ordered=None, dtype=None, fastpath=False)[source] -
Represents a categorical variable in classic R / S-plus fashion
Categoricalscan only take on only a limited, and usually fixed, number of possible values (categories). In contrast to statistical categorical variables, aCategoricalmight have an order, but numerical operations (additions, divisions, …) are not possible.All values of the
Categoricalare either incategoriesornp.nan. Assigning values outside ofcategorieswill 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.
dtype : CategoricalDtype
An instance of
CategoricalDtypeto use for this categoricalNew in version 0.21.0.
Raises: ValueError
If the categories do not validate.
TypeError
If an explicit
ordered=Trueis given but nocategoriesand thevaluesare not sortable.See also
-
pandas.api.types.CategoricalDtype - Type for categorical data
-
CategoricalIndex - An Index with an underlying
Categorical
Notes
See the user guide for more.
Examples
>>> pd.Categorical([1, 2, 3, 1, 2, 3]) [1, 2, 3, 1, 2, 3] Categories (3, int64): [1, 2, 3]
>>> pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c']) [a, b, c, a, b, c] Categories (3, object): [a, b, c]
Ordered
Categoricalscan be sorted according to the custom order of the categories and can have a min and max value.>>> c = pd.Categorical(['a','b','c','a','b','c'], ordered=True, ... categories=['c', 'b', 'a']) >>> c [a, b, c, a, b, c] Categories (3, object): [c < b < a] >>> c.min() 'c'
Attributes
categoriesThe categories of this categorical. codesThe category codes of this categorical. orderedWhether the categories have an ordered relationship dtypeThe CategoricalDtypefor this instanceMethods
from_codes(codes, categories[, ordered])Make a Categorical type from codes and categories arrays. __array__([dtype])The numpy array interface. -
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.Categorical.html