def create_index(self, categories=None, ordered=False): if categories is None: categories = list('cab') return CategoricalIndex(list('aabbca'), categories=categories, ordered=ordered)
def test_construction(self): ci = self.create_index(categories=list('abcd')) categories = ci.categories result = Index(ci) tm.assert_index_equal(result, ci, exact=True) self.assertFalse(result.ordered) result = Index(ci.values) tm.assert_index_equal(result, ci, exact=True) self.assertFalse(result.ordered) # empty result = CategoricalIndex(categories=categories) self.assertTrue(result.categories.equals(Index(categories))) tm.assert_numpy_array_equal(result.codes, np.array([], dtype='int8')) self.assertFalse(result.ordered) # passing categories result = CategoricalIndex(list('aabbca'), categories=categories) self.assertTrue(result.categories.equals(Index(categories))) tm.assert_numpy_array_equal(result.codes, np.array([0, 0, 1, 1, 2, 0], dtype='int8')) c = pd.Categorical(list('aabbca')) result = CategoricalIndex(c) self.assertTrue(result.categories.equals(Index(list('abc')))) tm.assert_numpy_array_equal(result.codes, np.array([0, 0, 1, 1, 2, 0], dtype='int8')) self.assertFalse(result.ordered) result = CategoricalIndex(c, categories=categories) self.assertTrue(result.categories.equals(Index(categories))) tm.assert_numpy_array_equal(result.codes, np.array([0, 0, 1, 1, 2, 0], dtype='int8')) self.assertFalse(result.ordered) ci = CategoricalIndex(c, categories=list('abcd')) result = CategoricalIndex(ci) self.assertTrue(result.categories.equals(Index(categories))) tm.assert_numpy_array_equal(result.codes, np.array([0, 0, 1, 1, 2, 0], dtype='int8')) self.assertFalse(result.ordered) result = CategoricalIndex(ci, categories=list('ab')) self.assertTrue(result.categories.equals(Index(list('ab')))) tm.assert_numpy_array_equal( result.codes, np.array([0, 0, 1, 1, -1, 0], dtype='int8')) self.assertFalse(result.ordered) result = CategoricalIndex(ci, categories=list('ab'), ordered=True) self.assertTrue(result.categories.equals(Index(list('ab')))) tm.assert_numpy_array_equal( result.codes, np.array([0, 0, 1, 1, -1, 0], dtype='int8')) self.assertTrue(result.ordered) # turn me to an Index result = Index(np.array(ci)) self.assertIsInstance(result, Index) self.assertNotIsInstance(result, CategoricalIndex)
def test_method_delegation(self): ci = CategoricalIndex(list('aabbca'), categories=list('cabdef')) result = ci.set_categories(list('cab')) tm.assert_index_equal( result, CategoricalIndex(list('aabbca'), categories=list('cab'))) ci = CategoricalIndex(list('aabbca'), categories=list('cab')) result = ci.rename_categories(list('efg')) tm.assert_index_equal( result, CategoricalIndex(list('ffggef'), categories=list('efg'))) ci = CategoricalIndex(list('aabbca'), categories=list('cab')) result = ci.add_categories(['d']) tm.assert_index_equal( result, CategoricalIndex(list('aabbca'), categories=list('cabd'))) ci = CategoricalIndex(list('aabbca'), categories=list('cab')) result = ci.remove_categories(['c']) tm.assert_index_equal( result, CategoricalIndex(list('aabb') + [np.nan] + ['a'], categories=list('ab'))) ci = CategoricalIndex(list('aabbca'), categories=list('cabdef')) result = ci.as_unordered() tm.assert_index_equal(result, ci) ci = CategoricalIndex(list('aabbca'), categories=list('cabdef')) result = ci.as_ordered() tm.assert_index_equal( result, CategoricalIndex(list('aabbca'), categories=list('cabdef'), ordered=True)) # invalid self.assertRaises(ValueError, lambda: ci.set_categories(list('cab'), inplace=True))