Skip to content

Commit d0d28fe

Browse files
matagusjorisvandenbossche
authored andcommitted
DOC: Improving docstring of take method (pandas-dev#16948)
1 parent 91245a7 commit d0d28fe

File tree

1 file changed

+63
-4
lines changed

1 file changed

+63
-4
lines changed

pandas/core/generic.py

+63-4
Original file line numberDiff line numberDiff line change
@@ -2063,18 +2063,77 @@ def __delitem__(self, key):
20632063

20642064
def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs):
20652065
"""
2066-
Analogous to ndarray.take
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.
20672071
20682072
Parameters
20692073
----------
2070-
indices : list / array of ints
2074+
indices : array-like
2075+
An array of ints indicating which positions to take.
20712076
axis : int, default 0
2072-
convert : translate neg to pos indices (default)
2073-
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, just as with
2081+
indexing into Python lists. For example, if `-1` was passed in,
2082+
this index would be converted ``n - 1``.
2083+
is_copy : bool, default True
2084+
Whether to return a copy of the original object or not.
2085+
2086+
Examples
2087+
--------
2088+
>>> df = pd.DataFrame([('falcon', 'bird', 389.0),
2089+
('parrot', 'bird', 24.0),
2090+
('lion', 'mammal', 80.5),
2091+
('monkey', 'mammal', np.nan)],
2092+
columns=('name', 'class', 'max_speed'),
2093+
index=[0, 2, 3, 1])
2094+
>>> df
2095+
name class max_speed
2096+
0 falcon bird 389.0
2097+
2 parrot bird 24.0
2098+
3 lion mammal 80.5
2099+
1 monkey mammal NaN
2100+
2101+
Take elements at positions 0 and 3 along the axis 0 (default).
2102+
2103+
Note how the actual indices selected (0 and 1) do not correspond to
2104+
our selected indices 0 and 3. That's because we are selecting the 0th
2105+
and 3rd rows, not rows whose indices equal 0 and 3.
2106+
2107+
>>> df.take([0, 3])
2108+
0 falcon bird 389.0
2109+
1 monkey mammal NaN
2110+
2111+
Take elements at indices 1 and 2 along the axis 1 (column selection).
2112+
2113+
>>> df.take([1, 2], axis=1)
2114+
class max_speed
2115+
0 bird 389.0
2116+
2 bird 24.0
2117+
3 mammal 80.5
2118+
1 mammal NaN
2119+
2120+
We may take elements using negative integers for positive indices,
2121+
starting from the end of the object, just like with Python lists.
2122+
2123+
>>> df.take([-1, -2])
2124+
name class max_speed
2125+
1 monkey mammal NaN
2126+
3 lion mammal 80.5
20742127
20752128
Returns
20762129
-------
20772130
taken : type of caller
2131+
An array-like containing the elements taken from the object.
2132+
2133+
See Also
2134+
--------
2135+
numpy.ndarray.take
2136+
numpy.take
20782137
"""
20792138
nv.validate_take(tuple(), kwargs)
20802139
self._consolidate_inplace()

0 commit comments

Comments
 (0)