def test_index_loc_to_iloc_c(self) -> None: idx = Index(('a', 'b', 'c', 'd')) with self.assertRaises(KeyError): _ = idx.loc_to_iloc(['c', 'd', 'e']) post = idx.loc_to_iloc(['c', 'd', 'e'], partial_selection=True) self.assertEqual(post, [2, 3])
def test_index_loc_to_iloc_a(self) -> None: idx = Index(('a', 'b', 'c', 'd')) self.assertEqual( tp.cast(np.ndarray, idx.loc_to_iloc(np.array([True, False, True, False]))).tolist(), [0, 2]) self.assertEqual(idx.loc_to_iloc(slice('c',)), slice(None, 3, None)) # type: ignore self.assertEqual(idx.loc_to_iloc(slice('b','d')), slice(1, 4, None)) # type: ignore self.assertEqual(idx.loc_to_iloc('d'), 3)
def test_index_loc_to_iloc_boolen_a(self): idx = Index(('a', 'b', 'c', 'd')) # unlike Pandas, both of these presently fail with self.assertRaises(KeyError): idx.loc_to_iloc([False, True]) with self.assertRaises(KeyError): idx.loc_to_iloc([False, True, False, True]) # but a Boolean array works post = idx.loc_to_iloc(np.array([False, True, False, True])) self.assertEqual(post.tolist(), [1, 3])
def test_index_loc_to_iloc_boolen_a(self): idx = Index(('a', 'b', 'c', 'd')) # returns nothing as index does not match anything post = idx.loc_to_iloc(Series([False, True, False, True])) self.assertTrue(len(post) == 0) post = idx.loc_to_iloc( Series([False, True, False, True], index=('b', 'c', 'd', 'a'))) self.assertEqual(post.tolist(), [0, 2]) post = idx.loc_to_iloc( Series([False, True, False, True], index=list('abcd'))) self.assertEqual(post.tolist(), [1, 3])
def test_index_creation_a(self) -> None: idx = Index(('a', 'b', 'c', 'd')) #idx2 = idx['b':'d'] self.assertEqual(idx.values.tolist(), ['a', 'b', 'c', 'd']) self.assertEqual(idx[2:].values.tolist(), ['c', 'd']) #type: ignore self.assertEqual( idx.loc['b':].values.tolist(), ['b', 'c', 'd' ]) # type: ignore # https://github.com/python/typeshed/pull/3024 self.assertEqual( idx.loc['b':'d'].values.tolist(), ['b', 'c', 'd' ]) # type: ignore # https://github.com/python/typeshed/pull/3024 self.assertEqual(idx.loc_to_iloc(['b', 'b', 'c']), [1, 1, 2]) self.assertEqual(idx.loc['c'], 'c') idxgo = IndexGO(('a', 'b', 'c', 'd')) self.assertEqual(idxgo.values.tolist(), ['a', 'b', 'c', 'd']) idxgo.append('e') self.assertEqual(idxgo.values.tolist(), ['a', 'b', 'c', 'd', 'e']) idxgo.extend(('f', 'g')) self.assertEqual(idxgo.values.tolist(), ['a', 'b', 'c', 'd', 'e', 'f', 'g'])
def test_index_creation_a(self): idx = Index(('a', 'b', 'c', 'd')) #idx2 = idx['b':'d'] self.assertEqual(idx.values.tolist(), ['a', 'b', 'c', 'd']) self.assertEqual(idx[2:].values.tolist(), ['c', 'd']) self.assertEqual(idx.loc['b':].values.tolist(), ['b', 'c', 'd']) self.assertEqual(idx.loc['b':'d'].values.tolist(), ['b', 'c', 'd']) self.assertEqual(idx.loc_to_iloc(['b', 'b', 'c']), [1, 1, 2]) self.assertEqual(idx.loc['c'].values.tolist(), ['c']) idxgo = IndexGO(('a', 'b', 'c', 'd')) self.assertEqual(idxgo.values.tolist(), ['a', 'b', 'c', 'd']) idxgo.append('e') self.assertEqual(idxgo.values.tolist(), ['a', 'b', 'c', 'd', 'e']) idxgo.extend(('f', 'g')) self.assertEqual(idxgo.values.tolist(), ['a', 'b', 'c', 'd', 'e', 'f', 'g'])
def test_index_loc_to_iloc_boolen_b(self) -> None: idx = Index(('a', 'b', 'c', 'd')) # returns nothing as index does not match anything post = idx.loc_to_iloc(Series([False, True, False, True])) self.assertTrue(len(tp.cast(tp.Sized, post)) == 0) post = idx.loc_to_iloc( Series([False, True, False, True], index=('b', 'c', 'd', 'a'))) assert isinstance(post, np.ndarray) self.assertEqual(post.tolist(), [0, 2]) post = idx.loc_to_iloc( Series([False, True, False, True], index=list('abcd'))) assert isinstance(post, np.ndarray) self.assertEqual(post.tolist(), [1, 3])
def test_index_iloc_loc_to_iloc_a(self) -> None: idx = Index(('a', 'b', 'c', 'd')) self.assertEqual(idx.loc_to_iloc(ILoc[1]), 1) self.assertEqual(idx.loc_to_iloc(ILoc[[0, 2]]), [0, 2])
def test_index_creation_b(self) -> None: idx = Index((x for x in ('a', 'b', 'c', 'd') if x in {'b', 'd'})) self.assertEqual(idx.loc_to_iloc('b'), 0) self.assertEqual(idx.loc_to_iloc('d'), 1)
def test_index_loc_to_iloc_b(self) -> None: idx = Index(('a', 'b', 'c', 'd')) post = idx.loc_to_iloc(Series(['b', 'c'])) self.assertEqual(post, [1, 2])