本文整理匯總了Python中pandas.core.indexing.IndexingError方法的典型用法代碼示例。如果您正苦於以下問題:Python indexing.IndexingError方法的具體用法?Python indexing.IndexingError怎麽用?Python indexing.IndexingError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas.core.indexing
的用法示例。
在下文中一共展示了indexing.IndexingError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_getitem_setitem_boolean_corner
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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
示例2: test_df_iloc_value_error
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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))
示例3: _parse_tuple
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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
示例4: test_getitem_boolean_empty
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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)]
示例5: test_getitem_setitem_fancy_exceptions
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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
示例6: process_loc_indexes
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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
示例7: test_getitem_boolean_empty
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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)
示例8: test_getitem_setitem_fancy_exceptions
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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
示例9: df_getitem_bool_series_idx_codegen
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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
示例10: df_getitem_int_iloc_codegen
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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
示例11: df_getitem_list_iloc_codegen
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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
示例12: test_getitem_boolean_empty
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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)
示例13: test_partial_slicing_with_multiindex
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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)
示例14: process_iloc_indexes
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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
示例15: sdc_reindex_series_overload
# 需要導入模塊: from pandas.core import indexing [as 別名]
# 或者: from pandas.core.indexing import IndexingError [as 別名]
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