本文整理汇总了Python中pandas.indexes.api.CategoricalIndex.astype方法的典型用法代码示例。如果您正苦于以下问题:Python CategoricalIndex.astype方法的具体用法?Python CategoricalIndex.astype怎么用?Python CategoricalIndex.astype使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.indexes.api.CategoricalIndex
的用法示例。
在下文中一共展示了CategoricalIndex.astype方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_equals_categorical
# 需要导入模块: from pandas.indexes.api import CategoricalIndex [as 别名]
# 或者: from pandas.indexes.api.CategoricalIndex import astype [as 别名]
def test_equals_categorical(self):
ci1 = CategoricalIndex(['a', 'b'], categories=['a', 'b'], ordered=True)
ci2 = CategoricalIndex(['a', 'b'], categories=['a', 'b', 'c'],
ordered=True)
self.assertTrue(ci1.equals(ci1))
self.assertFalse(ci1.equals(ci2))
self.assertTrue(ci1.equals(ci1.astype(object)))
self.assertTrue(ci1.astype(object).equals(ci1))
self.assertTrue((ci1 == ci1).all())
self.assertFalse((ci1 != ci1).all())
self.assertFalse((ci1 > ci1).all())
self.assertFalse((ci1 < ci1).all())
self.assertTrue((ci1 <= ci1).all())
self.assertTrue((ci1 >= ci1).all())
self.assertFalse((ci1 == 1).all())
self.assertTrue((ci1 == Index(['a', 'b'])).all())
self.assertTrue((ci1 == ci1.values).all())
# invalid comparisons
with tm.assertRaisesRegexp(ValueError, "Lengths must match"):
ci1 == Index(['a', 'b', 'c'])
self.assertRaises(TypeError, lambda: ci1 == ci2)
self.assertRaises(
TypeError, lambda: ci1 == Categorical(ci1.values, ordered=False))
self.assertRaises(
TypeError,
lambda: ci1 == Categorical(ci1.values, categories=list('abc')))
# tests
# make sure that we are testing for category inclusion properly
ci = CategoricalIndex(list('aabca'), categories=['c', 'a', 'b'])
self.assertFalse(ci.equals(list('aabca')))
self.assertFalse(ci.equals(CategoricalIndex(list('aabca'))))
self.assertTrue(ci.equals(ci.copy()))
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
ci = CategoricalIndex(list('aabca'),
categories=['c', 'a', 'b', np.nan])
self.assertFalse(ci.equals(list('aabca')))
self.assertFalse(ci.equals(CategoricalIndex(list('aabca'))))
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
self.assertTrue(ci.equals(ci.copy()))
ci = CategoricalIndex(list('aabca') + [np.nan],
categories=['c', 'a', 'b'])
self.assertFalse(ci.equals(list('aabca')))
self.assertFalse(ci.equals(CategoricalIndex(list('aabca'))))
self.assertTrue(ci.equals(ci.copy()))
ci = CategoricalIndex(list('aabca') + [np.nan],
categories=['c', 'a', 'b'])
self.assertFalse(ci.equals(list('aabca') + [np.nan]))
self.assertFalse(ci.equals(CategoricalIndex(list('aabca') + [np.nan])))
self.assertTrue(ci.equals(ci.copy()))