pandas.api.extensions.ExtensionArray.take#
- ExtensionArray.take(indices, *, allow_fill=False, fill_value=None)[源代码]#
从数组中获取元素。
- 参数
- indices整型序列或一维np.ndarray
要采用的指标。
- allow_fill布尔值,默认为False
如何处理中的负值 indices 。
False:中的负值 indices 从右侧指示位置索引(默认设置)。这类似于
numpy.take()
。True:中的负值 indices 指示缺少的值。这些值设置为 fill_value 。任何其他负值都会引发
ValueError
。
- fill_value任何,可选
用于NA索引的填充值 allow_fill 是真的。这可能是
None
,在这种情况下,类型的默认NA值,self.dtype.na_value
,被使用。对于许多ExtensionArray,将有两种表示形式 fill_value :一个面向用户的“盒装”标量,以及一个低级物理NA值。 fill_value 应该是面向用户的版本,如果需要,实现应该处理将其转换为物理版本以处理Take。
- 退货
- ExtensionArray
- 加薪
- IndexError
当索引超出数组的界限时。
- ValueError
什么时候 indices 包含负值而不是
-1
和 allow_fill 是真的。
参见
numpy.take
沿轴从数组中获取元素。
api.extensions.take
从数组中获取元素。
注意事项
ExtensionArray.Take由调用
Series.__getitem__
,.loc
,iloc
,何时 indices 是一系列的值。此外,它还由Series.reindex()
方法,或任何其他导致重新对齐的方法。 fill_value 。示例
下面是一个示例实现,它依赖于将扩展数组强制转换为对象dtype。这使用帮助器方法
pandas.api.extensions.take()
。def take(self, indices, allow_fill=False, fill_value=None): from pandas.core.algorithms import take # If the ExtensionArray is backed by an ndarray, then # just pass that here instead of coercing to object. data = self.astype(object) if allow_fill and fill_value is None: fill_value = self.dtype.na_value # fill value should always be translated from the scalar # type for the array, to the physical storage type for # the data, before passing to take. result = take(data, indices, fill_value=fill_value, allow_fill=allow_fill) return self._from_sequence(result, dtype=self.dtype)