class pandas.Categorical(values, categories=None, ordered=None, dtype=None, fastpath=False)
[source]
Represent 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, a Categorical
might have an order, but numerical operations (additions, divisions, …) are not possible.
All values of the Categorical
are either in categories
or np.nan
. Assigning values outside of categories
will raise a ValueError
. Order is defined by the order of the categories
, not lexical order of the values.
Parameters: |
|
---|---|
Raises: |
|
See also
api.types.CategoricalDtype
CategoricalIndex
Categorical
.See the user guide for more.
>>> 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 Categoricals
can 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'
categories | The categories of this categorical. |
codes | The category codes of this categorical. |
ordered | Whether the categories have an ordered relationship. |
dtype | The CategoricalDtype for this instance |
from_codes (codes[, categories, ordered, dtype]) | Make a Categorical type from codes and categories or dtype. |
__array__ (self[, 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.25.0/reference/api/pandas.Categorical.html