Skip to content

Commit e8f7bc3

Browse files
committed
DOC: Address reviewer comments
1 parent 0d49def commit e8f7bc3

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

pandas/core/generic.py

+40-20
Original file line numberDiff line numberDiff line change
@@ -2063,57 +2063,77 @@ def __delitem__(self, key):
20632063

20642064
def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs):
20652065
"""
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.
20682071
20692072
Parameters
20702073
----------
2071-
indices : list / array of ints
2074+
indices : array-like
2075+
An array of ints indicating which positions to take.
20722076
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, 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.
20752085
20762086
Examples
20772087
--------
2078-
>>> import numpy as np
2079-
>>> import pandas as pd
20802088
>>> df = pd.DataFrame([('falcon', 'bird', 389.0),
20812089
('parrot', 'bird', 24.0),
20822090
('lion', 'mammal', 80.5),
20832091
('monkey', 'mammal', np.nan)],
2084-
columns=('name', 'class', 'max_speed'))
2092+
columns=('name', 'class', 'max_speed'),
2093+
index=[0, 2, 3, 1])
20852094
>>> df
20862095
name class max_speed
20872096
0 falcon bird 389.0
2088-
1 parrot bird 24.0
2089-
2 lion mammal 80.5
2090-
3 monkey mammal NaN
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).
20912102
2092-
Take elements at indices 0 and 3 along the axis 0 (default)
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.
20932106
20942107
>>> df.take([0, 3])
20952108
0 falcon bird 389.0
2096-
3 monkey mammal NaN
2109+
1 monkey mammal NaN
20972110
2098-
Take elements at indices 1 and 2 along the axis 1
2111+
Take elements at indices 1 and 2 along the axis 1 (column selection).
20992112
21002113
>>> df.take([1, 2], axis=1)
21012114
class max_speed
21022115
0 bird 389.0
2103-
1 bird 24.0
2104-
2 mammal 80.5
2105-
3 mammal NaN
2116+
2 bird 24.0
2117+
3 mammal 80.5
2118+
1 mammal NaN
21062119
2107-
Also, we may take elements using negative integers for pos indices
2120+
We may take elements using negative integers for positive indices,
2121+
starting from the end of the object, just like with Python lists.
21082122
21092123
>>> df.take([-1, -2])
21102124
name class max_speed
2111-
3 monkey mammal NaN
2112-
2 lion mammal 80.5
2125+
1 monkey mammal NaN
2126+
3 lion mammal 80.5
21132127
21142128
Returns
21152129
-------
21162130
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
21172137
"""
21182138
nv.validate_take(tuple(), kwargs)
21192139
self._consolidate_inplace()

0 commit comments

Comments
 (0)