本文整理汇总了Python中numpy.ndarray.__getitem__方法的典型用法代码示例。如果您正苦于以下问题:Python ndarray.__getitem__方法的具体用法?Python ndarray.__getitem__怎么用?Python ndarray.__getitem__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.ndarray
的用法示例。
在下文中一共展示了ndarray.__getitem__方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __getitem__
# 需要导入模块: from numpy import ndarray [as 别名]
# 或者: from numpy.ndarray import __getitem__ [as 别名]
def __getitem__(self, indx):
"""
Get the index.
"""
m = self._mask
if isinstance(m[indx], ndarray):
# Can happen when indx is a multi-dimensional field:
# A = ma.masked_array(data=[([0,1],)], mask=[([True,
# False],)], dtype=[("A", ">i2", (2,))])
# x = A[0]; y = x["A"]; then y.mask["A"].size==2
# and we can not say masked/unmasked.
# The result is no longer mvoid!
# See also issue #6724.
return masked_array(
data=self._data[indx], mask=m[indx],
fill_value=self._fill_value[indx],
hard_mask=self._hardmask)
if m is not nomask and m[indx]:
return masked
return self._data[indx]
示例2: __getslice__
# 需要导入模块: from numpy import ndarray [as 别名]
# 或者: from numpy.ndarray import __getitem__ [as 别名]
def __getslice__(self, i, j):
"""
x.__getslice__(i, j) <==> x[i:j]
Return the slice described by (i, j). The use of negative indices
is not supported.
"""
return self.__getitem__(slice(i, j))
示例3: __getitem__
# 需要导入模块: from numpy import ndarray [as 别名]
# 或者: from numpy.ndarray import __getitem__ [as 别名]
def __getitem__(self, indx):
result = self.dataiter.__getitem__(indx).view(type(self.ma))
if self.maskiter is not None:
_mask = self.maskiter.__getitem__(indx)
if isinstance(_mask, ndarray):
# set shape to match that of data; this is needed for matrices
_mask.shape = result.shape
result._mask = _mask
elif isinstance(_mask, np.void):
return mvoid(result, mask=_mask, hardmask=self.ma._hardmask)
elif _mask: # Just a scalar, masked
return masked
return result
# This won't work if ravel makes a copy
示例4: __getitem__
# 需要导入模块: from numpy import ndarray [as 别名]
# 或者: from numpy.ndarray import __getitem__ [as 别名]
def __getitem__(self, key):
try:
result = self.index.get_value(self, key)
if not lib.isscalar(result):
if is_list_like(result) and not isinstance(result, Series):
# we need to box if we have a non-unique index here
# otherwise have inline ndarray/lists
if not self.index.is_unique:
result = self._constructor(
result, index=[key] * len(result),
dtype=self.dtype).__finalize__(self)
return result
except InvalidIndexError:
pass
except (KeyError, ValueError):
if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
# kludge
pass
elif key is Ellipsis:
return self
elif is_bool_indexer(key):
pass
else:
# we can try to coerce the indexer (or this will raise)
new_key = self.index._convert_scalar_indexer(key,
kind='getitem')
if type(new_key) != type(key):
return self.__getitem__(new_key)
raise
except Exception:
raise
if com.is_iterator(key):
key = list(key)
if is_bool_indexer(key):
key = check_bool_indexer(self.index, key)
return self._get_with(key)