pandas.Index.take#
- Index.take(indices, axis=0, allow_fill=<no_default>, fill_value=<no_default>, **kwargs)[source]#
Return a new Index of the values selected by the indices.
For internal compatibility with numpy arrays.
- Parameters:
- indicesarray-like
Indices to be taken.
- axis{0 or ‘index’}, optional
The axis over which to select values, always 0 or ‘index’.
- allow_fillbool, optional
How to handle negative values in indices.
False: negative values in indices indicate positional indices from the right, matching
numpy.take().True: negative values in indices indicate missing values.
-1entries are set tofill_value(using the default NA value of the caller’s dtype if not supplied). Any other negative values raise aValueError.Not supplied: defaults to
allow_fill=Falseunlessfill_valueis explicitly provided, in which case fill semantics apply (allow_fill=True).
- fill_valuescalar, optional
If fill semantics apply (see
allow_fill), indices specified by-1are filled withfill_value. Not specifyingfill_valueor passingfill_value=Nonewill fill using the default NA value of the caller’s dtype. If the Index’s dtype cannot holdfill_value, the result is promoted to a common dtype (e.g. an integer Index +np.nanfill is cast to float, while an integer Index + a string fill is cast to object).- **kwargs
Required for compatibility with numpy.
- Returns:
- Index
An index formed of elements at the given indices. Will be the same type as self, except for RangeIndex.
See also
numpy.ndarray.takeReturn an array formed from the elements of a at the given indices.
Examples
>>> idx = pd.Index(["a", "b", "c"]) >>> idx.take([2, 2, 1, 2]) Index(['c', 'c', 'b', 'c'], dtype='str')