def test_slice_locs_na_raises(self): index = Index([np.nan, 1, 2]) with pytest.raises(KeyError, match=""): index.slice_locs(start=1.5) with pytest.raises(KeyError, match=""): index.slice_locs(end=1.5)
def test_slice_locs_na(self): index = Index([np.nan, 1, 2]) assert index.slice_locs(1) == (1, 3) assert index.slice_locs(np.nan) == (0, 3) index = Index([0, np.nan, np.nan, 1, 2]) assert index.slice_locs(np.nan) == (1, 5)
def test_slice_locs_dup(self): index = Index(["a", "a", "b", "c", "d", "d"]) assert index.slice_locs("a", "d") == (0, 6) assert index.slice_locs(end="d") == (0, 6) assert index.slice_locs("a", "c") == (0, 4) assert index.slice_locs("b", "d") == (2, 6) index2 = index[::-1] assert index2.slice_locs("d", "a") == (0, 6) assert index2.slice_locs(end="a") == (0, 6) assert index2.slice_locs("d", "b") == (0, 4) assert index2.slice_locs("c", "a") == (2, 6)
def test_slice_locs(self, dtype): index = Index(np.array([0, 1, 2, 5, 6, 7, 9, 10], dtype=dtype)) n = len(index) assert index.slice_locs(start=2) == (2, n) assert index.slice_locs(start=3) == (3, n) assert index.slice_locs(3, 8) == (3, 6) assert index.slice_locs(5, 10) == (3, n) assert index.slice_locs(end=8) == (0, 6) assert index.slice_locs(end=9) == (0, 7) # reversed index2 = index[::-1] assert index2.slice_locs(8, 2) == (2, 6) assert index2.slice_locs(7, 3) == (2, 5)
def _calc_slice_param(cls, input_index_value: IndexValue, pd_index: pd.Index, inp, index: slice, axis: int) -> Dict: param = dict() if input_index_value.has_value(): start, end = pd_index.slice_locs(index.start, index.stop, index.step, kind='loc') slc = slice(start, end, index.step) size = calc_sliced_size(inp.shape[axis], slc) param['shape'] = size out_index = pd_index[slc] param['index_value'] = parse_index(out_index, store_data=axis == 1) if axis == 1: param['dtypes'] = inp.dtypes[slc] else: assert axis == 0 if index.start is None and index.stop is None: param['shape'] = calc_sliced_size(inp.shape[axis], index) else: param['shape'] = np.nan param['index_value'] = parse_index(pd_index, inp, index) return param
def test_slice_locs_negative_step(self, in_slice, expected): index = Index(list("bcdxy")) s_start, s_stop = index.slice_locs(in_slice.start, in_slice.stop, in_slice.step) result = index[s_start : s_stop : in_slice.step] expected = Index(list(expected)) tm.assert_index_equal(result, expected)
def test_slice_locs_dup_numeric(self, dtype): index = Index(np.array([10, 12, 12, 14], dtype=dtype)) assert index.slice_locs(12, 12) == (1, 3) assert index.slice_locs(11, 13) == (1, 3) index2 = index[::-1] assert index2.slice_locs(12, 12) == (1, 3) assert index2.slice_locs(13, 11) == (1, 3)
def test_slice_locs_float_locs(self, dtype): index = Index(np.array([0, 1, 2, 5, 6, 7, 9, 10], dtype=dtype)) n = len(index) assert index.slice_locs(5.0, 10.0) == (3, n) assert index.slice_locs(4.5, 10.5) == (3, 8) index2 = index[::-1] assert index2.slice_locs(8.5, 1.5) == (2, 6) assert index2.slice_locs(10.5, -1) == (0, n)
def _calc_slice_param( cls, input_index_value: IndexValue, pd_index: pd.Index, inp, index: slice, axis: int, ) -> Dict: param = dict() if is_full_slice(index): # full slice on this axis param["shape"] = inp.shape[axis] param["index_value"] = input_index_value if axis == 1: param["dtypes"] = inp.dtypes elif input_index_value.has_value(): start, end = pd_index.slice_locs(index.start, index.stop, index.step, kind="loc") slc = slice(start, end, index.step) size = calc_sliced_size(inp.shape[axis], slc) param["shape"] = size out_index = pd_index[slc] param["index_value"] = parse_index(out_index, store_data=axis == 1) if axis == 1: param["dtypes"] = inp.dtypes[slc] else: assert axis == 0 if index.start is None and index.stop is None: param["shape"] = calc_sliced_size(inp.shape[axis], index) else: param["shape"] = np.nan param["index_value"] = parse_index(pd_index, inp, index) return param