pandas.CategoricalDtype#

class pandas.CategoricalDtype(categories=None, ordered=False)[源代码]#

具有类别和有序性的类别数据的类型。

Parameters:
categoriessequence, optional

必须是唯一的,并且不能包含任何 null。类别存储在 Index 中,如果提供了 index,则将使用该 index 的 dtype。

orderedbool or None, default False

此类别是否被视为有序类别。None 可用于在组合类别的操作(例如 astype)中维护现有类别的有序值,并且如果不存在现有的有序值需要维护,则将解析为 False。

参见

Categorical

以经典的 R / S-plus 风格表示分类变量。

Notes

此类有助于独立于值指定 Categorical 的类型。有关更多信息,请参阅 CategoricalDtype

Examples

>>> t = pd.CategoricalDtype(categories=['b', 'a'], ordered=True)
>>> pd.Series(['a', 'b', 'a', 'c'], dtype=t)
0      a
1      b
2      a
3    NaN
dtype: category
Categories (2, object): ['b' < 'a']

可以通过提供一个空的 index 来创建一个具有特定 dtype 的空 CategoricalDtype。如下所示:

>>> pd.CategoricalDtype(pd.DatetimeIndex([])).categories.dtype
dtype('<M8[ns]')

Attributes

categories 

一个包含允许的唯一类别的 Index

ordered 

分类是否具有有序关系。

Methods

None