38
38
from pandas .core .index import (Index , MultiIndex , _ensure_index ,
39
39
InvalidIndexError )
40
40
import pandas .core .indexing as indexing
41
+ from pandas .core .indexing import maybe_convert_indices
41
42
from pandas .core .indexes .datetimes import DatetimeIndex
42
43
from pandas .core .indexes .period import PeriodIndex , Period
43
44
from pandas .core .internals import BlockManager
@@ -1822,7 +1823,8 @@ def _iget_item_cache(self, item):
1822
1823
if ax .is_unique :
1823
1824
lower = self ._get_item_cache (ax [item ])
1824
1825
else :
1825
- lower = self .take (item , axis = self ._info_axis_number , convert = True )
1826
+ lower = self ._take (item , axis = self ._info_axis_number ,
1827
+ convert = True )
1826
1828
return lower
1827
1829
1828
1830
def _box_item_values (self , key , values ):
@@ -2057,8 +2059,63 @@ def __delitem__(self, key):
2057
2059
except KeyError :
2058
2060
pass
2059
2061
2060
- def take (self , indices , axis = 0 , convert = True , is_copy = True , ** kwargs ):
2062
+ _shared_docs ['_take' ] = """
2063
+ Return the elements in the given *positional* indices along an axis.
2064
+
2065
+ This means that we are not indexing according to actual values in
2066
+ the index attribute of the object. We are indexing according to the
2067
+ actual position of the element in the object.
2068
+
2069
+ This is the internal version of ``.take()`` and will contain a wider
2070
+ selection of parameters useful for internal use but not as suitable
2071
+ for public usage.
2072
+
2073
+ Parameters
2074
+ ----------
2075
+ indices : array-like
2076
+ An array of ints indicating which positions to take.
2077
+ axis : int, default 0
2078
+ The axis on which to select elements. "0" means that we are
2079
+ selecting rows, "1" means that we are selecting columns, etc.
2080
+ convert : bool, default True
2081
+ Whether to convert negative indices into positive ones.
2082
+ For example, ``-1`` would map to the ``len(axis) - 1``.
2083
+ The conversions are similar to the behavior of indexing a
2084
+ regular Python list.
2085
+ is_copy : bool, default True
2086
+ Whether to return a copy of the original object or not.
2087
+
2088
+ Returns
2089
+ -------
2090
+ taken : type of caller
2091
+ An array-like containing the elements taken from the object.
2092
+
2093
+ See Also
2094
+ --------
2095
+ numpy.ndarray.take
2096
+ numpy.take
2061
2097
"""
2098
+
2099
+ @Appender (_shared_docs ['_take' ])
2100
+ def _take (self , indices , axis = 0 , convert = True , is_copy = True ):
2101
+ self ._consolidate_inplace ()
2102
+
2103
+ if convert :
2104
+ indices = maybe_convert_indices (indices , len (self ._get_axis (axis )))
2105
+
2106
+ new_data = self ._data .take (indices ,
2107
+ axis = self ._get_block_manager_axis (axis ),
2108
+ verify = True )
2109
+ result = self ._constructor (new_data ).__finalize__ (self )
2110
+
2111
+ # Maybe set copy if we didn't actually change the index.
2112
+ if is_copy :
2113
+ if not result ._get_axis (axis ).equals (self ._get_axis (axis )):
2114
+ result ._set_is_copy (self )
2115
+
2116
+ return result
2117
+
2118
+ _shared_docs ['take' ] = """
2062
2119
Return the elements in the given *positional* indices along an axis.
2063
2120
2064
2121
This means that we are not indexing according to actual values in
@@ -2073,9 +2130,12 @@ def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs):
2073
2130
The axis on which to select elements. "0" means that we are
2074
2131
selecting rows, "1" means that we are selecting columns, etc.
2075
2132
convert : bool, default True
2076
- Whether to convert negative indices to positive ones, just as with
2077
- indexing into Python lists. For example, if `-1` was passed in,
2078
- this index would be converted ``n - 1``.
2133
+ .. deprecated:: 0.21.0
2134
+
2135
+ Whether to convert negative indices into positive ones.
2136
+ For example, ``-1`` would map to the ``len(axis) - 1``.
2137
+ The conversions are similar to the behavior of indexing a
2138
+ regular Python list.
2079
2139
is_copy : bool, default True
2080
2140
Whether to return a copy of the original object or not.
2081
2141
@@ -2131,19 +2191,17 @@ class max_speed
2131
2191
numpy.ndarray.take
2132
2192
numpy.take
2133
2193
"""
2194
+
2195
+ @Appender (_shared_docs ['take' ])
2196
+ def take (self , indices , axis = 0 , convert = True , is_copy = True , ** kwargs ):
2134
2197
nv .validate_take (tuple (), kwargs )
2135
- self ._consolidate_inplace ()
2136
- new_data = self ._data .take (indices ,
2137
- axis = self ._get_block_manager_axis (axis ),
2138
- convert = True , verify = True )
2139
- result = self ._constructor (new_data ).__finalize__ (self )
2140
2198
2141
- # maybe set copy if we didn't actually change the index
2142
- if is_copy :
2143
- if not result . _get_axis ( axis ). equals ( self . _get_axis ( axis )):
2144
- result . _set_is_copy ( self )
2199
+ if not convert :
2200
+ msg = ( "The 'convert' parameter is deprecated "
2201
+ "and will be removed in a future version." )
2202
+ warnings . warn ( msg , FutureWarning , stacklevel = 2 )
2145
2203
2146
- return result
2204
+ return self . _take ( indices , axis = axis , convert = convert , is_copy = is_copy )
2147
2205
2148
2206
def xs (self , key , axis = 0 , level = None , drop_level = True ):
2149
2207
"""
@@ -2244,9 +2302,9 @@ def xs(self, key, axis=0, level=None, drop_level=True):
2244
2302
if isinstance (loc , np .ndarray ):
2245
2303
if loc .dtype == np .bool_ :
2246
2304
inds , = loc .nonzero ()
2247
- return self .take (inds , axis = axis , convert = False )
2305
+ return self ._take (inds , axis = axis , convert = False )
2248
2306
else :
2249
- return self .take (loc , axis = axis , convert = True )
2307
+ return self ._take (loc , axis = axis , convert = True )
2250
2308
2251
2309
if not is_scalar (loc ):
2252
2310
new_index = self .index [loc ]
@@ -5112,7 +5170,7 @@ def at_time(self, time, asof=False):
5112
5170
"""
5113
5171
try :
5114
5172
indexer = self .index .indexer_at_time (time , asof = asof )
5115
- return self .take (indexer , convert = False )
5173
+ return self ._take (indexer , convert = False )
5116
5174
except AttributeError :
5117
5175
raise TypeError ('Index must be DatetimeIndex' )
5118
5176
@@ -5136,7 +5194,7 @@ def between_time(self, start_time, end_time, include_start=True,
5136
5194
indexer = self .index .indexer_between_time (
5137
5195
start_time , end_time , include_start = include_start ,
5138
5196
include_end = include_end )
5139
- return self .take (indexer , convert = False )
5197
+ return self ._take (indexer , convert = False )
5140
5198
except AttributeError :
5141
5199
raise TypeError ('Index must be DatetimeIndex' )
5142
5200
0 commit comments