Python pandas.core.indexing.IndexingError() Examples

The following are 24 code examples of pandas.core.indexing.IndexingError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pandas.core.indexing , or try the search function .
Example #1
Source File: test_boolean.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_getitem_setitem_boolean_corner(test_data):
    ts = test_data.ts
    mask_shifted = ts.shift(1, freq=BDay()) > ts.median()

    # these used to raise...??

    msg = (r"Unalignable boolean Series provided as indexer \(index of"
           r" the boolean Series and of the indexed object do not match")
    with pytest.raises(IndexingError, match=msg):
        ts[mask_shifted]
    with pytest.raises(IndexingError, match=msg):
        ts[mask_shifted] = 1

    with pytest.raises(IndexingError, match=msg):
        ts.loc[mask_shifted]
    with pytest.raises(IndexingError, match=msg):
        ts.loc[mask_shifted] = 1 
Example #2
Source File: indexing.py    From modin with Apache License 2.0 6 votes vote down vote up
def _parse_tuple(tup):
    """Unpack the user input for getitem and setitem and compute ndim

    loc[a] -> ([a], :), 1D
    loc[[a,b],] -> ([a,b], :),
    loc[a,b] -> ([a], [b]), 0D
    """
    row_loc, col_loc = slice(None), slice(None)

    if is_tuple(tup):
        row_loc = tup[0]
        if len(tup) == 2:
            col_loc = tup[1]
        if len(tup) > 2:
            raise IndexingError("Too many indexers")
    else:
        row_loc = tup

    ndim = _compute_ndim(row_loc, col_loc)
    row_scaler = is_scalar(row_loc)
    col_scaler = is_scalar(col_loc)
    row_loc = [row_loc] if row_scaler else row_loc
    col_loc = [col_loc] if col_scaler else col_loc

    return row_loc, col_loc, ndim, row_scaler, col_scaler 
Example #3
Source File: test_dataframe.py    From sdc with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_df_iloc_value_error(self):
        def int_impl(df):
            return df.iloc[11]

        def list_impl(df):
            return df.iloc[[7, 14]]

        def list_bool_impl(df):
            return df.iloc[[True, False]]

        msg1 = 'Index is out of bounds for axis'
        msg2 = 'Item wrong length'
        df = pd.DataFrame({"A": [3.2, 4.4, 7.0, 3.3, 1.0],
                           "B": [5.5, np.nan, 3, 0, 7.7],
                           "C": [3, 4, 1, 0, 222]})

        impls = [(int_impl, msg1), (list_impl, msg1), (list_bool_impl, msg2)]
        for impl, msg in impls:
            with self.subTest(case=impl, msg=msg):
                func = self.jit(impl)
                with self.assertRaises(IndexingError) as raises:
                    func(df)
                self.assertIn(msg, str(raises.exception)) 
Example #4
Source File: test_boolean.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_getitem_setitem_boolean_corner(test_data):
    ts = test_data.ts
    mask_shifted = ts.shift(1, freq=BDay()) > ts.median()

    # these used to raise...??

    msg = (r"Unalignable boolean Series provided as indexer \(index of"
           r" the boolean Series and of the indexed object do not match")
    with pytest.raises(IndexingError, match=msg):
        ts[mask_shifted]
    with pytest.raises(IndexingError, match=msg):
        ts[mask_shifted] = 1

    with pytest.raises(IndexingError, match=msg):
        ts.loc[mask_shifted]
    with pytest.raises(IndexingError, match=msg):
        ts.loc[mask_shifted] = 1 
Example #5
Source File: test_boolean.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_getitem_boolean_empty():
    s = Series([], dtype=np.int64)
    s.index.name = 'index_name'
    s = s[s.isna()]
    assert s.index.name == 'index_name'
    assert s.dtype == np.int64

    # GH5877
    # indexing with empty series
    s = Series(['A', 'B'])
    expected = Series(np.nan, index=['C'], dtype=object)
    result = s[Series(['C'], dtype=object)]
    assert_series_equal(result, expected)

    s = Series(['A', 'B'])
    expected = Series(dtype=object, index=Index([], dtype='int64'))
    result = s[Series([], dtype=object)]
    assert_series_equal(result, expected)

    # invalid because of the boolean indexer
    # that's empty or not-aligned
    msg = (r"Unalignable boolean Series provided as indexer \(index of"
           r" the boolean Series and of the indexed object do not match")
    with pytest.raises(IndexingError, match=msg):
        s[Series([], dtype=bool)]

    with pytest.raises(IndexingError, match=msg):
        s[Series([True], dtype=bool)] 
Example #6
Source File: test_indexing.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_getitem_setitem_fancy_exceptions(self):
        ix = self.frame.iloc
        with tm.assert_raises_regex(IndexingError, 'Too many indexers'):
            ix[:, :, :]

        with pytest.raises(IndexingError):
            ix[:, :, :] = 1 
Example #7
Source File: test_boolean.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_getitem_boolean_empty():
    s = Series([], dtype=np.int64)
    s.index.name = 'index_name'
    s = s[s.isna()]
    assert s.index.name == 'index_name'
    assert s.dtype == np.int64

    # GH5877
    # indexing with empty series
    s = Series(['A', 'B'])
    expected = Series(np.nan, index=['C'], dtype=object)
    result = s[Series(['C'], dtype=object)]
    assert_series_equal(result, expected)

    s = Series(['A', 'B'])
    expected = Series(dtype=object, index=Index([], dtype='int64'))
    result = s[Series([], dtype=object)]
    assert_series_equal(result, expected)

    # invalid because of the boolean indexer
    # that's empty or not-aligned
    def f():
        s[Series([], dtype=bool)]

    pytest.raises(IndexingError, f)

    def f():
        s[Series([True], dtype=bool)]

    pytest.raises(IndexingError, f) 
Example #8
Source File: test_indexing.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_getitem_setitem_fancy_exceptions(self):
        ix = self.frame.iloc
        with tm.assert_raises_regex(IndexingError, 'Too many indexers'):
            ix[:, :, :]

        with pytest.raises(IndexingError):
            ix[:, :, :] = 1 
Example #9
Source File: test_indexing.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_getitem_boolean_empty(self):
        s = Series([], dtype=np.int64)
        s.index.name = 'index_name'
        s = s[s.isna()]
        assert s.index.name == 'index_name'
        assert s.dtype == np.int64

        # GH5877
        # indexing with empty series
        s = Series(['A', 'B'])
        expected = Series(np.nan, index=['C'], dtype=object)
        result = s[Series(['C'], dtype=object)]
        assert_series_equal(result, expected)

        s = Series(['A', 'B'])
        expected = Series(dtype=object, index=Index([], dtype='int64'))
        result = s[Series([], dtype=object)]
        assert_series_equal(result, expected)

        # invalid because of the boolean indexer
        # that's empty or not-aligned
        def f():
            s[Series([], dtype=bool)]

        pytest.raises(IndexingError, f)

        def f():
            s[Series([True], dtype=bool)]

        pytest.raises(IndexingError, f) 
Example #10
Source File: hpat_pandas_dataframe_functions.py    From sdc with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def df_getitem_list_iloc_codegen(self, idx):
    """
    Example of generated implementation:
        def _df_getitem_list_iloc_impl(self, idx):
            check_idx = False
            for i in idx:
                if -1 < i < len(self._dataframe.index):
                    check_idx = True
            if check_idx == True:
                data_0 = pandas.Series(self._dataframe._data[0][0])
                result_0 = data_0.iloc[numpy.array(idx)]
                data_1 = pandas.Series(self._dataframe._data[1][0])
                result_1 = data_1.iloc[numpy.array(idx)]
                return pandas.DataFrame(data={"A": result_0, "B": result_1}, index=idx)
            raise IndexingError('Index is out of bounds for axis')
    """
    func_lines = ['def _df_getitem_list_iloc_impl(self, idx):',
                  '  check_idx = False',
                  '  for i in idx:',
                  '    if -1 < i < len(self._dataframe.index):',
                  '      check_idx = True',
                  '  if check_idx == True:']
    results = []
    index = '[self._dataframe._index[i] for i in idx]'
    if isinstance(self.index, types.NoneType):
        index = 'idx'
    for i, c in enumerate(self.columns):
        col_loc = self.column_loc[c]
        type_id, col_id = col_loc.type_id, col_loc.col_id
        result_c = f"result_{i}"
        func_lines += [f"    data_{i} = pandas.Series(self._dataframe._data[{type_id}][{col_id}])",
                       f"    {result_c} = data_{i}.iloc[numpy.array(idx)]"]
        results.append((c, result_c))
    data = ', '.join(f'"{col}": {data}' for col, data in results)
    func_lines += [f"    return pandas.DataFrame(data={{{data}}}, index={index})",
                   f"  raise IndexingError('Index is out of bounds for axis')"]

    func_text = '\n'.join(func_lines)
    global_vars = {'pandas': pandas, 'numpy': numpy, 'IndexingError': IndexingError}

    return func_text, global_vars 
Example #11
Source File: hpat_pandas_dataframe_functions.py    From sdc with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def df_getitem_int_iloc_codegen(self, idx):
    """
    Example of generated implementation:
        def _df_getitem_int_iloc_impl(self, idx):
            if -1 < idx < len(self._dataframe.index):
                data_0 = pandas.Series(self._dataframe._data[0][0])
                result_0 = data_0.iat[idx]
                data_1 = pandas.Series(self._dataframe._data[0][1])
                result_1 = data_1.iat[idx]
                return pandas.Series(data=[result_0, result_1], index=['A', 'B'], name=str(idx))
            raise IndexingError('Index is out of bounds for axis')
    """
    func_lines = ['def _df_getitem_int_iloc_impl(self, idx):',
                  '  if -1 < idx < len(self._dataframe.index):']
    results = []
    index = []
    name = 'self._dataframe._index[idx]'
    if isinstance(self.index, types.NoneType):
        name = 'idx'
    for i, c in enumerate(self.columns):
        col_loc = self.column_loc[c]
        type_id, col_id = col_loc.type_id, col_loc.col_id
        result_c = f"result_{i}"
        func_lines += [f"    data_{i} = pandas.Series(self._dataframe._data[{type_id}][{col_id}])",
                       f"    {result_c} = data_{i}.iat[idx]"]
        results.append(result_c)
        index.append(c)
    data = ', '.join(col for col in results)
    func_lines += [f"    return pandas.Series(data=[{data}], index={index}, name=str({name}))",
                   f"  raise IndexingError('Index is out of bounds for axis')"]

    func_text = '\n'.join(func_lines)
    global_vars = {'pandas': pandas, 'numpy': numpy, 'IndexingError': IndexingError}

    return func_text, global_vars 
Example #12
Source File: hpat_pandas_dataframe_functions.py    From sdc with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def df_getitem_bool_series_idx_codegen(self, idx):
    """
    Example of generated implementation with provided index:
        def _df_getitem_bool_series_idx_impl(self, idx):
          length = len(self._data[0][0])
          self_index = range(len(self._data[0][0]))
          if length > len(idx):
            msg = "Unalignable boolean Series provided as indexer " + \
                  "(index of the boolean Series and of the indexed object do not match)."
            raise IndexingError(msg)
          # do not trim idx._data to length as getitem_by_mask handles such case
          res_index = getitem_by_mask(self_index, idx._data)
          # df index is default, same as positions so it can be used in take
          data_0 = self._data[0][0]
          res_data_0 = sdc_take(data_0, res_index)
          data_1 = self._data[1][0]
          res_data_1 = sdc_take(data_1, res_index)
          return pandas.DataFrame({"A": res_data_0, "B": res_data_1}, index=res_index)
    """
    func_lines = ['def _df_getitem_bool_series_idx_impl(self, idx):']
    func_lines += df_getitem_bool_series_idx_main_codelines(self, idx)
    func_text = '\n'.join(func_lines)
    global_vars = {'pandas': pandas, 'numpy': numpy,
                   'getitem_by_mask': getitem_by_mask,
                   'sdc_take': _sdc_take,
                   'sdc_reindex_series': sdc_reindex_series,
                   'IndexingError': IndexingError}

    return func_text, global_vars 
Example #13
Source File: test_indexing.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_getitem_setitem_fancy_exceptions(self):
        ix = self.frame.iloc
        with pytest.raises(IndexingError, match='Too many indexers'):
            ix[:, :, :]

        with pytest.raises(IndexingError):
            ix[:, :, :] = 1 
Example #14
Source File: test_boolean.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_getitem_boolean_empty():
    s = Series([], dtype=np.int64)
    s.index.name = 'index_name'
    s = s[s.isna()]
    assert s.index.name == 'index_name'
    assert s.dtype == np.int64

    # GH5877
    # indexing with empty series
    s = Series(['A', 'B'])
    expected = Series(np.nan, index=['C'], dtype=object)
    result = s[Series(['C'], dtype=object)]
    assert_series_equal(result, expected)

    s = Series(['A', 'B'])
    expected = Series(dtype=object, index=Index([], dtype='int64'))
    result = s[Series([], dtype=object)]
    assert_series_equal(result, expected)

    # invalid because of the boolean indexer
    # that's empty or not-aligned
    msg = (r"Unalignable boolean Series provided as indexer \(index of"
           r" the boolean Series and of the indexed object do not match")
    with pytest.raises(IndexingError, match=msg):
        s[Series([], dtype=bool)]

    with pytest.raises(IndexingError, match=msg):
        s[Series([True], dtype=bool)] 
Example #15
Source File: test_indexing.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_getitem_setitem_fancy_exceptions(self):
        ix = self.frame.iloc
        with tm.assert_raises_regex(IndexingError, 'Too many indexers'):
            ix[:, :, :]

        with pytest.raises(IndexingError):
            ix[:, :, :] = 1 
Example #16
Source File: test_boolean.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_getitem_boolean_empty():
    s = Series([], dtype=np.int64)
    s.index.name = 'index_name'
    s = s[s.isna()]
    assert s.index.name == 'index_name'
    assert s.dtype == np.int64

    # GH5877
    # indexing with empty series
    s = Series(['A', 'B'])
    expected = Series(np.nan, index=['C'], dtype=object)
    result = s[Series(['C'], dtype=object)]
    assert_series_equal(result, expected)

    s = Series(['A', 'B'])
    expected = Series(dtype=object, index=Index([], dtype='int64'))
    result = s[Series([], dtype=object)]
    assert_series_equal(result, expected)

    # invalid because of the boolean indexer
    # that's empty or not-aligned
    def f():
        s[Series([], dtype=bool)]

    pytest.raises(IndexingError, f)

    def f():
        s[Series([True], dtype=bool)]

    pytest.raises(IndexingError, f) 
Example #17
Source File: loc.py    From mars with Apache License 2.0 5 votes vote down vote up
def process_loc_indexes(inp, indexes):
    ndim = inp.ndim

    if not isinstance(indexes, tuple):
        indexes = (indexes,)
    if len(indexes) < ndim:
        indexes += (slice(None),) * (ndim - len(indexes))
    if len(indexes) > ndim:
        raise IndexingError('Too many indexers')

    new_indexes = []
    for ax, index in enumerate(indexes):
        if isinstance(index, (list, np.ndarray, pd.Series, Base, Entity)):
            if not isinstance(index, (Base, Entity)):
                index = np.asarray(index)
            else:
                index = asarray(index)
                if ax == 1:
                    # do not support tensor index on axis 1
                    # because if so, the dtypes and columns_value would be unknown
                    try:
                        index = index.fetch()
                    except (RuntimeError, ValueError):
                        raise NotImplementedError('indexer on axis columns cannot be '
                                                  'non-executed tensor')
        new_indexes.append(index)

    return new_indexes 
Example #18
Source File: test_indexing.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_getitem_setitem_fancy_exceptions(self):
        ix = self.frame.iloc
        with pytest.raises(IndexingError, match='Too many indexers'):
            ix[:, :, :]

        with pytest.raises(IndexingError):
            ix[:, :, :] = 1 
Example #19
Source File: common_functions.py    From sdc with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def sdc_reindex_series_overload(arr, index, name, by_index):
    """ Reindexes series data by new index following the logic of pandas.core.indexing.check_bool_indexer """

    range_indexes = isinstance(index, RangeIndexType) and isinstance(by_index, RangeIndexType)
    data_dtype, index_dtype = arr.dtype, index.dtype
    data_is_str_arr = isinstance(arr.dtype, types.UnicodeType)

    def sdc_reindex_series_impl(arr, index, name, by_index):

        # no reindexing is needed if indexes are equal
        if range_indexes == True:  # noqa
            equal_indexes = numpy_like.array_equal(index, by_index)
        else:
            equal_indexes = False
        if (index is by_index or equal_indexes):
            return pandas.Series(data=arr, index=by_index, name=name)

        if data_is_str_arr == True:  # noqa
            _res_data = [''] * len(by_index)
            res_data_nan_mask = numpy.zeros(len(by_index), dtype=types.bool_)
        else:
            _res_data = numpy.empty(len(by_index), dtype=data_dtype)

        # build a dict of self.index values to their positions:
        map_index_to_position = Dict.empty(
            key_type=index_dtype,
            value_type=types.int32
        )

        for i, value in enumerate(index):
            if value in map_index_to_position:
                raise ValueError("cannot reindex from a duplicate axis")
            else:
                map_index_to_position[value] = i

        index_mismatch = 0
        # FIXME: TypingError in parfor step (wrong promotion to float64?) if prange is used
        for i in numpy.arange(len(by_index)):
            if by_index[i] in map_index_to_position:
                pos_in_self = map_index_to_position[by_index[i]]
                _res_data[i] = arr[pos_in_self]
                if data_is_str_arr == True:  # noqa
                    res_data_nan_mask[i] = isna(arr, i)
            else:
                index_mismatch += 1
        if index_mismatch:
            msg = "Unalignable boolean Series provided as indexer " + \
                  "(index of the boolean Series and of the indexed object do not match)."
            raise IndexingError(msg)

        if data_is_str_arr == True:  # noqa
            res_data = create_str_arr_from_list(_res_data)
            str_arr_set_na_by_mask(res_data, res_data_nan_mask)
        else:
            res_data = _res_data

        return pandas.Series(data=res_data, index=by_index, name=name)

    return sdc_reindex_series_impl 
Example #20
Source File: hpat_pandas_dataframe_functions.py    From sdc with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def df_getitem_bool_series_idx_main_codelines(self, idx):
    """Generate main code lines for df.getitem"""
    length_expr = df_length_expr(self)

    # optimization for default indexes in df and idx when index alignment is trivial
    if (isinstance(self.index, types.NoneType) and isinstance(idx.index, types.NoneType)):
        func_lines = [f'  length = {length_expr}',
                      f'  self_index = self.index',
                      f'  if length > len(idx):',
                      f'    msg = "Unalignable boolean Series provided as indexer " + \\',
                      f'          "(index of the boolean Series and of the indexed object do not match)."',
                      f'    raise IndexingError(msg)',
                      f'  # do not trim idx._data to length as getitem_by_mask handles such case',
                      f'  res_index = getitem_by_mask(self_index, idx._data)',
                      f'  # df index is default, same as positions so it can be used in take']
        results = []
        for i, col in enumerate(self.columns):
            col_loc = self.column_loc[col]
            type_id, col_id = col_loc.type_id, col_loc.col_id
            res_data = f'res_data_{i}'
            func_lines += [
                f'  data_{i} = self._data[{type_id}][{col_id}]',
                f'  {res_data} = sdc_take(data_{i}, res_index)'
            ]
            results.append((col, res_data))

        data = ', '.join(f'"{col}": {data}' for col, data in results)
        func_lines += [
            f'  return pandas.DataFrame({{{data}}}, index=res_index)'
        ]
    else:
        func_lines = [
            f'  length = {length_expr}',
            f'  self_index = self.index',
            f'  reindexed_idx = sdc_reindex_series(idx._data, idx.index, idx._name, self_index)',
            f'  res_index = getitem_by_mask(self_index, reindexed_idx._data)',
            f'  selected_pos = getitem_by_mask(numpy.arange(length), reindexed_idx._data)'
        ]
        results = []
        for i, col in enumerate(self.columns):
            col_loc = self.column_loc[col]
            type_id, col_id = col_loc.type_id, col_loc.col_id
            res_data = f'res_data_{i}'
            func_lines += [
                f'  data_{i} = self._data[{type_id}][{col_id}]',
                f'  {res_data} = sdc_take(data_{i}, selected_pos)'
            ]
            results.append((col, res_data))

        data = ', '.join(f'"{col}": {data}' for col, data in results)
        func_lines += [
            f'  return pandas.DataFrame({{{data}}}, index=res_index)'
        ]

    return func_lines 
Example #21
Source File: test_partial_slicing.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 4 votes vote down vote up
def test_partial_slicing_with_multiindex(self):

        # GH 4758
        # partial string indexing with a multi-index buggy
        df = DataFrame({'ACCOUNT': ["ACCT1", "ACCT1", "ACCT1", "ACCT2"],
                        'TICKER': ["ABC", "MNP", "XYZ", "XYZ"],
                        'val': [1, 2, 3, 4]},
                       index=date_range("2013-06-19 09:30:00",
                                        periods=4, freq='5T'))
        df_multi = df.set_index(['ACCOUNT', 'TICKER'], append=True)

        expected = DataFrame([
            [1]
        ], index=Index(['ABC'], name='TICKER'), columns=['val'])
        result = df_multi.loc[('2013-06-19 09:30:00', 'ACCT1')]
        tm.assert_frame_equal(result, expected)

        expected = df_multi.loc[
            (pd.Timestamp('2013-06-19 09:30:00', tz=None), 'ACCT1', 'ABC')]
        result = df_multi.loc[('2013-06-19 09:30:00', 'ACCT1', 'ABC')]
        tm.assert_series_equal(result, expected)

        # this is an IndexingError as we don't do partial string selection on
        # multi-levels.
        def f():
            df_multi.loc[('2013-06-19', 'ACCT1', 'ABC')]

        pytest.raises(IndexingError, f)

        # GH 4294
        # partial slice on a series mi
        s = pd.DataFrame(np.random.rand(1000, 1000), index=pd.date_range(
            '2000-1-1', periods=1000)).stack()

        s2 = s[:-1].copy()
        expected = s2['2000-1-4']
        result = s2[pd.Timestamp('2000-1-4')]
        tm.assert_series_equal(result, expected)

        result = s[pd.Timestamp('2000-1-4')]
        expected = s['2000-1-4']
        tm.assert_series_equal(result, expected)

        df2 = pd.DataFrame(s)
        expected = df2.xs('2000-1-4')
        result = df2.loc[pd.Timestamp('2000-1-4')]
        tm.assert_frame_equal(result, expected) 
Example #22
Source File: iloc.py    From mars with Apache License 2.0 4 votes vote down vote up
def process_iloc_indexes(inp, indexes):
    ndim = inp.ndim

    if not isinstance(indexes, tuple):
        indexes = (indexes,)
    if len(indexes) < ndim:
        indexes += (slice(None),) * (ndim - len(indexes))
    if len(indexes) > ndim:
        raise IndexingError('Too many indexers')

    new_indexes = []
    # check each index
    for ax, index in enumerate(indexes):
        if isinstance(index, tuple):
            # a tuple should already have been caught by this point
            # so don't treat a tuple as a valid indexer
            raise IndexingError("Too many indexers")
        elif isinstance(index, slice):
            if any(v is not None for v in [index.start, index.stop, index.step]):
                pd_index = (inp.index_value if ax == 0 else inp.columns_value).to_pandas()
                for val in [index.start, index.stop, index.step]:
                    if val is not None:
                        try:
                            pd_index[val]  # check on the pandas
                        except IndexError:
                            pass
                        except TypeError:
                            raise TypeError(
                                'cannot do slice indexing on {} '
                                'with these indexers [{}] '
                                'of {}'.format(type(pd_index), val, type(val)))
            new_indexes.append(index)
        elif isinstance(index, (list, np.ndarray, pd.Series, Base, Entity)):
            if not isinstance(index, (Base, Entity)):
                index = np.asarray(index)
            else:
                index = asarray(index)
                if ax == 1:
                    # do not support tensor index on axis 1
                    # because if so, the dtypes and columns_value would be unknown
                    try:
                        index = index.fetch()
                    except (RuntimeError, ValueError):
                        raise NotImplementedError('indexer on axis columns cannot be '
                                                  'non-executed tensor')
            if index.dtype != np.bool_:
                index = index.astype(np.int64)
            if index.ndim != 1:
                raise ValueError('Buffer has wrong number of dimensions '
                                 '(expected 1, got {})'.format(index.ndim))
            new_indexes.append(index)
        elif isinstance(index, Integral):
            shape = inp.shape[ax]
            if not np.isnan(shape):
                if index < -shape or index >= shape:
                    raise IndexError('single positional indexer is out-of-bounds')
            new_indexes.append(index)
        else:
            raise ValueError(_ILOC_ERROR_MSG)

    return new_indexes 
Example #23
Source File: test_partial_slicing.py    From coffeegrindsize with MIT License 4 votes vote down vote up
def test_partial_slicing_with_multiindex(self):

        # GH 4758
        # partial string indexing with a multi-index buggy
        df = DataFrame({'ACCOUNT': ["ACCT1", "ACCT1", "ACCT1", "ACCT2"],
                        'TICKER': ["ABC", "MNP", "XYZ", "XYZ"],
                        'val': [1, 2, 3, 4]},
                       index=date_range("2013-06-19 09:30:00",
                                        periods=4, freq='5T'))
        df_multi = df.set_index(['ACCOUNT', 'TICKER'], append=True)

        expected = DataFrame([
            [1]
        ], index=Index(['ABC'], name='TICKER'), columns=['val'])
        result = df_multi.loc[('2013-06-19 09:30:00', 'ACCT1')]
        tm.assert_frame_equal(result, expected)

        expected = df_multi.loc[
            (pd.Timestamp('2013-06-19 09:30:00', tz=None), 'ACCT1', 'ABC')]
        result = df_multi.loc[('2013-06-19 09:30:00', 'ACCT1', 'ABC')]
        tm.assert_series_equal(result, expected)

        # this is an IndexingError as we don't do partial string selection on
        # multi-levels.
        def f():
            df_multi.loc[('2013-06-19', 'ACCT1', 'ABC')]

        pytest.raises(IndexingError, f)

        # GH 4294
        # partial slice on a series mi
        s = pd.DataFrame(np.random.rand(1000, 1000), index=pd.date_range(
            '2000-1-1', periods=1000)).stack()

        s2 = s[:-1].copy()
        expected = s2['2000-1-4']
        result = s2[pd.Timestamp('2000-1-4')]
        tm.assert_series_equal(result, expected)

        result = s[pd.Timestamp('2000-1-4')]
        expected = s['2000-1-4']
        tm.assert_series_equal(result, expected)

        df2 = pd.DataFrame(s)
        expected = df2.xs('2000-1-4')
        result = df2.loc[pd.Timestamp('2000-1-4')]
        tm.assert_frame_equal(result, expected) 
Example #24
Source File: test_partial_slicing.py    From recruit with Apache License 2.0 4 votes vote down vote up
def test_partial_slicing_with_multiindex(self):

        # GH 4758
        # partial string indexing with a multi-index buggy
        df = DataFrame({'ACCOUNT': ["ACCT1", "ACCT1", "ACCT1", "ACCT2"],
                        'TICKER': ["ABC", "MNP", "XYZ", "XYZ"],
                        'val': [1, 2, 3, 4]},
                       index=date_range("2013-06-19 09:30:00",
                                        periods=4, freq='5T'))
        df_multi = df.set_index(['ACCOUNT', 'TICKER'], append=True)

        expected = DataFrame([
            [1]
        ], index=Index(['ABC'], name='TICKER'), columns=['val'])
        result = df_multi.loc[('2013-06-19 09:30:00', 'ACCT1')]
        tm.assert_frame_equal(result, expected)

        expected = df_multi.loc[
            (pd.Timestamp('2013-06-19 09:30:00', tz=None), 'ACCT1', 'ABC')]
        result = df_multi.loc[('2013-06-19 09:30:00', 'ACCT1', 'ABC')]
        tm.assert_series_equal(result, expected)

        # this is an IndexingError as we don't do partial string selection on
        # multi-levels.
        def f():
            df_multi.loc[('2013-06-19', 'ACCT1', 'ABC')]

        pytest.raises(IndexingError, f)

        # GH 4294
        # partial slice on a series mi
        s = pd.DataFrame(np.random.rand(1000, 1000), index=pd.date_range(
            '2000-1-1', periods=1000)).stack()

        s2 = s[:-1].copy()
        expected = s2['2000-1-4']
        result = s2[pd.Timestamp('2000-1-4')]
        tm.assert_series_equal(result, expected)

        result = s[pd.Timestamp('2000-1-4')]
        expected = s['2000-1-4']
        tm.assert_series_equal(result, expected)

        df2 = pd.DataFrame(s)
        expected = df2.xs('2000-1-4')
        result = df2.loc[pd.Timestamp('2000-1-4')]
        tm.assert_frame_equal(result, expected)