pandas.CategoricalIndex#

class pandas.CategoricalIndex(data=None, categories=None, ordered=None, dtype=None, copy=False, name=None)[源代码]#

基于底层 Categorical 的 Index。

CategoricalIndexCategorical 一样,只能接受有限的、通常是固定的可能值(categories)。此外,像 Categorical 一样,它可能有顺序,但不能进行数值运算(加法、除法等)。

Parameters:
dataarray-like (1-dimensional)

类别的(分类的)值。如果给出了 categories,则不在 categories 中的值将被替换为 NaN。

categoriesindex-like, optional

类别的类别。项必须是唯一的。如果此处未给出类别(并且也不在 dtype 中),则将从 data 中推断出类别。

orderedbool, optional

此类别是否被视为有序类别。如果此处或 dtype 中未给出,则生成的类别将是无序的。

dtypeCategoricalDtype 或 “category”, optional

如果为 CategoricalDtype ,则不能与 categoriesordered 一起使用。

copybool,默认 False

复制输入 ndarray。

nameobject, optional

要存储在索引中的名称。

Raises:
ValueError

如果分类未通过验证。

TypeError

如果显式给出 ordered=True 但没有 categories,并且 values 不可排序。

参见

Index

pandas Index 的基类。

Categorical

一个分类数组。

CategoricalDtype

分类数据类型。

Notes

有关更多信息,请参阅 user guide

Examples

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

CategoricalIndex 也可以从 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')

有序的 CategoricalIndex 可以具有最小值和最大值。

>>> 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')
>>> ci.min()
'c'

Attributes

codes

此分类索引的分类代码。

categories

此分类的分类。

ordered

分类是否具有有序关系。

Methods

rename_categories (*args, **kwargs)

重命名类别。

reorder_categories (*args, **kwargs)

按照 new_categories 中指定的顺序重新排列类别。

add_categories (*args, **kwargs)

添加新类别。

remove_categories (*args, **kwargs)

删除指定的类别。

remove_unused_categories (*args, **kwargs)

删除未使用的类别。

set_categories (*args, **kwargs)

将类别设置为指定的 new_categories。

as_ordered (*args, **kwargs)

将 Categorical 设置为有序。

as_unordered (*args, **kwargs)

将 Categorical 设置为无序。

map (mapper[, na_action])

使用输入映射或函数映射值。