@@ -2063,57 +2063,73 @@ def __delitem__(self, key):
2063
2063
2064
2064
def take (self , indices , axis = 0 , convert = True , is_copy = True , ** kwargs ):
2065
2065
"""
2066
- Return an object formed from the elements in the given indices along an
2067
- axis
2066
+ Return the elements in the given *positional* indices along an axis.
2067
+
2068
+ This means that we are not indexing according to actual values in
2069
+ the index attribute of the object. We are indexing according to the
2070
+ actual position of the element in the object's array of values.
2068
2071
2069
2072
Parameters
2070
2073
----------
2071
- indices : list / array of ints
2074
+ indices : array-like
2075
+ An array of ints indicating which positions to take.
2072
2076
axis : int, default 0
2073
- convert : translate neg to pos indices (default)
2074
- is_copy : mark the returned frame as a copy
2077
+ The axis on which to select elements. "0" means that we are
2078
+ selecting rows, "1" means that we are selecting columns, etc.
2079
+ convert : bool, default True
2080
+ Whether to convert negative indices to positive ones.
2081
+ is_copy : bool, default True
2082
+ Whether to return a copy of the original object or not.
2075
2083
2076
2084
Examples
2077
2085
--------
2078
- >>> import numpy as np
2079
- >>> import pandas as pd
2080
2086
>>> df = pd.DataFrame([('falcon', 'bird', 389.0),
2081
2087
('parrot', 'bird', 24.0),
2082
2088
('lion', 'mammal', 80.5),
2083
2089
('monkey', 'mammal', np.nan)],
2084
- columns=('name', 'class', 'max_speed'))
2090
+ columns=('name', 'class', 'max_speed'),
2091
+ index=[0, 3, 2, 1])
2085
2092
>>> df
2086
2093
name class max_speed
2087
2094
0 falcon bird 389.0
2088
- 1 parrot bird 24.0
2095
+ 3 parrot bird 24.0
2089
2096
2 lion mammal 80.5
2090
- 3 monkey mammal NaN
2097
+ 1 monkey mammal NaN
2091
2098
2092
- Take elements at indices 0 and 3 along the axis 0 (default)
2099
+ Take elements at positions 0 and 3 along the axis 0 (default).
2100
+
2101
+ Note how the actual indices selected (0 and 1) do not correspond to
2102
+ our selected indices 0 and 3. That's because we are selecting the 0th
2103
+ and 3rd rows, not rows whose indices equal 0 and 3.
2093
2104
2094
2105
>>> df.take([0, 3])
2095
2106
0 falcon bird 389.0
2096
- 3 monkey mammal NaN
2107
+ 1 monkey mammal NaN
2097
2108
2098
2109
Take elements at indices 1 and 2 along the axis 1
2099
2110
2100
2111
>>> df.take([1, 2], axis=1)
2101
2112
class max_speed
2102
2113
0 bird 389.0
2103
- 1 bird 24.0
2114
+ 3 bird 24.0
2104
2115
2 mammal 80.5
2105
- 3 mammal NaN
2116
+ 1 mammal NaN
2106
2117
2107
- Also, we may take elements using negative integers for pos indices
2118
+ We may take elements using negative integers for positive indices.
2108
2119
2109
2120
>>> df.take([-1, -2])
2110
2121
name class max_speed
2111
- 3 monkey mammal NaN
2122
+ 1 monkey mammal NaN
2112
2123
2 lion mammal 80.5
2113
2124
2114
2125
Returns
2115
2126
-------
2116
2127
taken : type of caller
2128
+ An array-like containing the elements taken from the object.
2129
+
2130
+ See Also
2131
+ --------
2132
+ ndarray.take
2117
2133
"""
2118
2134
nv .validate_take (tuple (), kwargs )
2119
2135
self ._consolidate_inplace ()
0 commit comments