def test_append_to_another(self): # hits _concat_index_asobject fst = Index(['a', 'b']) snd = CategoricalIndex(['d', 'e']) result = fst.append(snd) expected = Index(['a', 'b', 'd', 'e']) tm.assert_index_equal(result, expected)
def test_reindexing(self): ci = self.create_index() oidx = Index(np.array(ci)) for n in [1, 2, 5, len(ci)]: finder = oidx[np.random.randint(0, len(ci), size=n)] expected = oidx.get_indexer_non_unique(finder)[0] actual = ci.get_indexer(finder) tm.assert_numpy_array_equal(expected, actual)
def test_reindexing(self): np.random.seed(123456789) ci = self.create_index() oidx = Index(np.array(ci)) for n in [1, 2, 5, len(ci)]: finder = oidx[np.random.randint(0, len(ci), size=n)] expected = oidx.get_indexer_non_unique(finder)[0] actual = ci.get_indexer(finder) tm.assert_numpy_array_equal(expected, actual) # see gh-17323 # # Even when indexer is equal to the # members in the index, we should # respect duplicates instead of taking # the fast-track path. for finder in [list("aabbca"), list("aababca")]: expected = oidx.get_indexer_non_unique(finder)[0] actual = ci.get_indexer(finder) tm.assert_numpy_array_equal(expected, actual)
def test_get_loc(self): # GH 12531 cidx1 = CategoricalIndex(list('abcde'), categories=list('edabc')) idx1 = Index(list('abcde')) assert cidx1.get_loc('a') == idx1.get_loc('a') assert cidx1.get_loc('e') == idx1.get_loc('e') for i in [cidx1, idx1]: with pytest.raises(KeyError): i.get_loc('NOT-EXIST') # non-unique cidx2 = CategoricalIndex(list('aacded'), categories=list('edabc')) idx2 = Index(list('aacded')) # results in bool array res = cidx2.get_loc('d') tm.assert_numpy_array_equal(res, idx2.get_loc('d')) tm.assert_numpy_array_equal(res, np.array([False, False, False, True, False, True])) # unique element results in scalar res = cidx2.get_loc('e') assert res == idx2.get_loc('e') assert res == 4 for i in [cidx2, idx2]: with pytest.raises(KeyError): i.get_loc('NOT-EXIST') # non-unique, slicable cidx3 = CategoricalIndex(list('aabbb'), categories=list('abc')) idx3 = Index(list('aabbb')) # results in slice res = cidx3.get_loc('a') assert res == idx3.get_loc('a') assert res == slice(0, 2, None) res = cidx3.get_loc('b') assert res == idx3.get_loc('b') assert res == slice(2, 5, None) for i in [cidx3, idx3]: with pytest.raises(KeyError): i.get_loc('c')
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) assert not result.ordered result = Index(ci.values) tm.assert_index_equal(result, ci, exact=True) assert not result.ordered # empty result = CategoricalIndex(categories=categories) tm.assert_index_equal(result.categories, Index(categories)) tm.assert_numpy_array_equal(result.codes, np.array([], dtype='int8')) assert not result.ordered # passing categories result = CategoricalIndex(list('aabbca'), categories=categories) tm.assert_index_equal(result.categories, 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) tm.assert_index_equal(result.categories, Index(list('abc'))) tm.assert_numpy_array_equal(result.codes, np.array([0, 0, 1, 1, 2, 0], dtype='int8')) assert not result.ordered result = CategoricalIndex(c, categories=categories) tm.assert_index_equal(result.categories, Index(categories)) tm.assert_numpy_array_equal(result.codes, np.array([0, 0, 1, 1, 2, 0], dtype='int8')) assert not result.ordered ci = CategoricalIndex(c, categories=list('abcd')) result = CategoricalIndex(ci) tm.assert_index_equal(result.categories, Index(categories)) tm.assert_numpy_array_equal(result.codes, np.array([0, 0, 1, 1, 2, 0], dtype='int8')) assert not result.ordered result = CategoricalIndex(ci, categories=list('ab')) tm.assert_index_equal(result.categories, Index(list('ab'))) tm.assert_numpy_array_equal( result.codes, np.array([0, 0, 1, 1, -1, 0], dtype='int8')) assert not result.ordered result = CategoricalIndex(ci, categories=list('ab'), ordered=True) tm.assert_index_equal(result.categories, Index(list('ab'))) tm.assert_numpy_array_equal( result.codes, np.array([0, 0, 1, 1, -1, 0], dtype='int8')) assert result.ordered result = pd.CategoricalIndex(ci, categories=list('ab'), ordered=True) expected = pd.CategoricalIndex(ci, categories=list('ab'), ordered=True, dtype='category') tm.assert_index_equal(result, expected, exact=True) # turn me to an Index result = Index(np.array(ci)) assert isinstance(result, Index) assert not isinstance(result, CategoricalIndex)