W3cubDocs

/pandas 0.25

pandas.CategoricalIndex

class pandas.CategoricalIndex [source]

Index based on an underlying Categorical.

CategoricalIndex, like Categorical, can only take on a limited, and usually fixed, number of possible values (categories). Also, like Categorical, it might have an order, but numerical operations (additions, divisions, …) are not possible.

Parameters:
data : array-like (1-dimensional)

The values of the categorical. If categories are given, values not in categories will be replaced with NaN.

categories : index-like, optional

The categories for the categorical. Items need to be unique. If the categories are not given here (and also not in dtype), they will be inferred from the data.

ordered : bool, optional

Whether or not this categorical is treated as an ordered categorical. If not given here or in dtype, the resulting categorical will be unordered.

dtype : CategoricalDtype or the string “category”, optional

If CategoricalDtype, cannot be used together with categories or ordered.

New in version 0.21.0.

copy : bool, default False

Make a copy of input ndarray.

name : object, optional

Name to be stored in the index.

Raises:
ValueError

If the categories do not validate.

TypeError

If an explicit ordered=True is given but no categories and the values are not sortable.

See also

Index
The base pandas Index type.
Categorical
A categorical array.
CategoricalDtype
Type for categorical data.

Notes

See the user guide for more.

Examples

>>> pd.CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'])
CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'], categories=['a', 'b', 'c'], ordered=False, dtype='category')  # noqa

CategoricalIndex can also be instantiated from a Categorical:

>>> c = pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c'])
>>> pd.CategoricalIndex(c)
CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'], categories=['a', 'b', 'c'], ordered=False, dtype='category')  # noqa

Ordered CategoricalIndex can have a min and max value.

>>> ci = pd.CategoricalIndex(['a','b','c','a','b','c'], ordered=True,
...                          categories=['c', 'b', 'a'])
>>> ci
CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'], categories=['c', 'b', 'a'], ordered=True, dtype='category')  # noqa
>>> ci.min()
'c'

Attributes

codes
categories
ordered

Methods

rename_categories(self, \*args, \*\*kwargs) Rename categories.
reorder_categories(self, \*args, \*\*kwargs) Reorder categories as specified in new_categories.
add_categories(self, \*args, \*\*kwargs) Add new categories.
remove_categories(self, \*args, \*\*kwargs) Remove the specified categories.
remove_unused_categories(self, \*args, …) Remove categories which are not used.
set_categories(self, \*args, \*\*kwargs) Set the categories to the specified new_categories.
as_ordered(self, \*args, \*\*kwargs) Set the Categorical to be ordered.
as_unordered(self, \*args, \*\*kwargs) Set the Categorical to be unordered.
map(self, mapper) Map values using input correspondence (a dict, Series, or function).

© 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.CategoricalIndex.html