pandas.CategoricalIndex.equals#

CategoricalIndex.equals(other)[源代码]#

确定两个 CategoricalIndex 对象是否包含相同的元素。

Returns:
bool

如果两个 pandas.CategoricalIndex 对象具有相等的元素,则为 True,否则为 False

Examples

>>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'])
>>> ci2 = pd.CategoricalIndex(pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c']))
>>> ci.equals(ci2)
True

元素的顺序很重要。

>>> ci3 = pd.CategoricalIndex(['c', 'b', 'a', 'a', 'b', 'c'])
>>> ci.equals(ci3)
False

有序性也很重要。

>>> ci4 = ci.as_ordered()
>>> ci.equals(ci4)
False

类别很重要,但类别的顺序仅在 ordered=True 时才重要。

>>> ci5 = ci.set_categories(['a', 'b', 'c', 'd'])
>>> ci.equals(ci5)
False
>>> ci6 = ci.set_categories(['b', 'c', 'a'])
>>> ci.equals(ci6)
True
>>> ci_ordered = pd.CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'],
...                                  ordered=True)
>>> ci2_ordered = ci_ordered.set_categories(['b', 'c', 'a'])
>>> ci_ordered.equals(ci2_ordered)
False