def take(self, indexer, allow_fill=False, fill_value=None): from pandas.api.extensions import take # we always fill with 1 internally # to avoid upcasting data_fill_value = 1 if isna(fill_value) else fill_value result = take(self._data, indexer, fill_value=data_fill_value, allow_fill=allow_fill) mask = take(self._mask, indexer, fill_value=True, allow_fill=allow_fill) # if we are filling # we only fill where the indexer is null # not existing missing values # TODO(jreback) what if we have a non-na float as a fill value? if allow_fill and notna(fill_value): fill_mask = np.asarray(indexer) == -1 result[fill_mask] = fill_value mask = mask ^ fill_mask return type(self)(result, mask, copy=False)
def take(self, indices, allow_fill=False, fill_value=None): from pandas.api.extensions import take result = take(self.data, indices, allow_fill=allow_fill, fill_value=fill_value) return MyArry(result)
def take(self, indices, allow_fill=False, fill_value=None): data = self._data.to_pandas() if allow_fill and fill_value is None: fill_value = self.dtype.na_value result = take(data, indices, fill_value=fill_value, allow_fill=allow_fill) return self._from_sequence(result, dtype=self.dtype)
def take(self, indexer, allow_fill=False, fill_value=None): if fill_value is None: fill_value = 0 took = take(self.data, indexer, allow_fill=allow_fill, fill_value=fill_value) return type(self)(took)
def take(self, indexer, allow_fill=False, fill_value=None): from pandas.api.extensions import take data = self._data if allow_fill and fill_value is None: fill_value = self.dtype.na_value result = take(data, indexer, fill_value=fill_value, allow_fill=allow_fill) return self._from_sequence(result)
def take(self, indices, allow_fill=False, fill_value=None): from pandas.api.extensions import take result = take(self.data, indices, allow_fill=allow_fill, fill_value=fill_value) if allow_fill and fill_value is None: result[pd.isna(result)] = None return StairsArray(result)
def take(self, indices, allow_fill=False, fill_value=None): from pandas.api.extensions import take if allow_fill: if fill_value is None or pd.isna(fill_value): fill_value = 0 result = take(self.data, indices, allow_fill=allow_fill, fill_value=fill_value) if fill_value == 0: result[result == 0] = None return GeometryArray(result)
def take(self, indices, allow_fill=False, fill_value=None): from pandas.api.extensions import take if allow_fill: if fill_value is None or pd.isna(fill_value): fill_value = None elif not is_scalar_geometry(fill_value): raise TypeError("Expected None or geometry fill value") result = take(self.data, indices, allow_fill=allow_fill, fill_value=fill_value) if allow_fill and fill_value is None: result[pd.isna(result)] = None return GeoArray(result)
def take(self, indexer, allow_fill=False, fill_value=None): """Take elements from an array. Relies on the take method defined in pandas: https://github.com/pandas-dev/pandas/blob/e246c3b05924ac1fe083565a765ce847fcad3d91/pandas/core/algorithms.py#L1483 """ from pandas.api.extensions import take data = self._data if allow_fill and fill_value is None: fill_value = self.dtype.na_value result = take( data, indexer, fill_value=fill_value, allow_fill=allow_fill) return self._from_sequence(result)
def take(self, indices, allow_fill=False, fill_value=None): from pandas.api.extensions import take if allow_fill: if fill_value is None or pd.isna(fill_value): fill_value = None elif isinstance(fill_value, BaseGeometry): fill_value = _shapely_to_geom(fill_value) elif not _is_scalar_geometry(fill_value): raise TypeError("provide geometry or None as fill value") result = take(self.data, indices, allow_fill=allow_fill, fill_value=fill_value) if allow_fill and fill_value is None: result[pd.isna(result)] = None return GeometryArray(result, crs=self.crs)
def take(self, indices, allow_fill=False, fill_value=None): """ Take elements (=rows) from the TimeArray. Parameters ---------- indexer : sequence of int The indices in `self` to take. The meaning of negative values in `indexer` depends on the value of `allow_fill`. allow_fill : bool, default False How to handle negative values in `indexer`. * False: negative values in `indices` indicate positional indices from the right. This is similar to :func:`numpy.take`. * True: negative values in `indices` indicate missing values (the default). These values are set to `fill_value`. Any other other negative values raise a ``ValueError``. fill_value : object The value to use for `indices` that are missing (-1), when ``allow_fill=True``. Returns ------- TimeArray See Also -------- Series.take : Similar method for Series. numpy.ndarray.take : Similar method for NumPy arrays. """ # TODO: revisit and simplify # Use the take implementation from pandas to get the takes separately # for the data and the time indices from pandas.api.extensions import take if not allow_fill and self.data.shape[0] == 0: # Quick-fix to raise the correct error when shape (0,0) # TODO: look into why np.ndarray.take() raises the wrong error when # it has shape (0,0) raise IndexError("cannot do a non-empty take from an empty axes.") data = take(self.data, indices, allow_fill=allow_fill) time_index = take(self.time_index, indices, allow_fill=allow_fill) if allow_fill and isinstance(fill_value, TimeBase): # Fill those indices that were out of range with the fill TimeBase indices = np.asarray(indices, dtype=np.intp) out_of_range = (indices < 0) | (indices >= data.shape[0]) data[rows_na(data, axis=1) & out_of_range] = fill_value.data time_index[rows_na(time_index, axis=1) & out_of_range] = \ fill_value.time_index elif allow_fill and fill_value is not None: TypeError(f"Only TimeBase are allowed as fill values, " f"got {type(fill_value)}") if data.shape[0] != 0 and np.all(np.isnan(data)): # If the take resulted in a missing TimeArray (note, this is # different from a length 0 TimeArray) # TODO: put in a separate function if needed elsewhere return self._constructor(empty((data.shape[0], 0))) return self._constructor(data, time_index)