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
@@ -1820,7 +1821,7 @@ def _iget_item_cache(self, item):
1820
1821
if ax .is_unique :
1821
1822
lower = self ._get_item_cache (ax [item ])
1822
1823
else :
1823
- lower = self .take (item , axis = self ._info_axis_number , convert = True )
1824
+ lower = self .take (item , axis = self ._info_axis_number )
1824
1825
return lower
1825
1826
1826
1827
def _box_item_values (self , key , values ):
@@ -2055,8 +2056,63 @@ def __delitem__(self, key):
2055
2056
except KeyError :
2056
2057
pass
2057
2058
2058
- def take (self , indices , axis = 0 , convert = True , is_copy = True , ** kwargs ):
2059
+ _shared_docs ['_take' ] = """
2060
+ Return the elements in the given *positional* indices along an axis.
2061
+
2062
+ This means that we are not indexing according to actual values in
2063
+ the index attribute of the object. We are indexing according to the
2064
+ actual position of the element in the object.
2065
+
2066
+ This is the internal version of ``.take()`` and will contain a wider
2067
+ selection of parameters useful for internal use but not as suitable
2068
+ for public usage.
2069
+
2070
+ Parameters
2071
+ ----------
2072
+ indices : array-like
2073
+ An array of ints indicating which positions to take.
2074
+ axis : int, default 0
2075
+ The axis on which to select elements. "0" means that we are
2076
+ selecting rows, "1" means that we are selecting columns, etc.
2077
+ convert : bool, default True
2078
+ Whether to convert negative indices into positive ones.
2079
+ For example, ``-1`` would map to the ``len(axis) - 1``.
2080
+ The conversions are similar to the behavior of indexing a
2081
+ regular Python list.
2082
+ is_copy : bool, default True
2083
+ Whether to return a copy of the original object or not.
2084
+
2085
+ Returns
2086
+ -------
2087
+ taken : type of caller
2088
+ An array-like containing the elements taken from the object.
2089
+
2090
+ See Also
2091
+ --------
2092
+ numpy.ndarray.take
2093
+ numpy.take
2059
2094
"""
2095
+
2096
+ @Appender (_shared_docs ['_take' ])
2097
+ def _take (self , indices , axis = 0 , convert = True , is_copy = False ):
2098
+ self ._consolidate_inplace ()
2099
+
2100
+ if convert :
2101
+ indices = maybe_convert_indices (indices , len (self ._get_axis (axis )))
2102
+
2103
+ new_data = self ._data .take (indices ,
2104
+ axis = self ._get_block_manager_axis (axis ),
2105
+ verify = True )
2106
+ result = self ._constructor (new_data ).__finalize__ (self )
2107
+
2108
+ # Maybe set copy if we didn't actually change the index.
2109
+ if is_copy :
2110
+ if not result ._get_axis (axis ).equals (self ._get_axis (axis )):
2111
+ result ._set_is_copy (self )
2112
+
2113
+ return result
2114
+
2115
+ _shared_docs ['take' ] = """
2060
2116
Return the elements in the given *positional* indices along an axis.
2061
2117
2062
2118
This means that we are not indexing according to actual values in
@@ -2071,9 +2127,12 @@ def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs):
2071
2127
The axis on which to select elements. "0" means that we are
2072
2128
selecting rows, "1" means that we are selecting columns, etc.
2073
2129
convert : bool, default True
2074
- Whether to convert negative indices to positive ones, just as with
2075
- indexing into Python lists. For example, if `-1` was passed in,
2076
- this index would be converted ``n - 1``.
2130
+ .. deprecated:: 0.21.0
2131
+
2132
+ Whether to convert negative indices into positive ones.
2133
+ For example, ``-1`` would map to the ``len(axis) - 1``.
2134
+ The conversions are similar to the behavior of indexing a
2135
+ regular Python list.
2077
2136
is_copy : bool, default True
2078
2137
Whether to return a copy of the original object or not.
2079
2138
@@ -2129,19 +2188,17 @@ class max_speed
2129
2188
numpy.ndarray.take
2130
2189
numpy.take
2131
2190
"""
2191
+
2192
+ @Appender (_shared_docs ['take' ])
2193
+ def take (self , indices , axis = 0 , convert = True , is_copy = True , ** kwargs ):
2132
2194
nv .validate_take (tuple (), kwargs )
2133
- self ._consolidate_inplace ()
2134
- new_data = self ._data .take (indices ,
2135
- axis = self ._get_block_manager_axis (axis ),
2136
- convert = True , verify = True )
2137
- result = self ._constructor (new_data ).__finalize__ (self )
2138
2195
2139
- # maybe set copy if we didn't actually change the index
2140
- if is_copy :
2141
- if not result . _get_axis ( axis ). equals ( self . _get_axis ( axis )):
2142
- result . _set_is_copy ( self )
2196
+ if not convert :
2197
+ msg = ( "The 'convert' parameter is deprecated "
2198
+ "and will be removed in a future version." )
2199
+ warnings . warn ( msg , FutureWarning , stacklevel = 2 )
2143
2200
2144
- return result
2201
+ return self . _take ( indices , axis = axis , convert = convert , is_copy = is_copy )
2145
2202
2146
2203
def xs (self , key , axis = 0 , level = None , drop_level = True ):
2147
2204
"""
@@ -2242,9 +2299,9 @@ def xs(self, key, axis=0, level=None, drop_level=True):
2242
2299
if isinstance (loc , np .ndarray ):
2243
2300
if loc .dtype == np .bool_ :
2244
2301
inds , = loc .nonzero ()
2245
- return self .take (inds , axis = axis , convert = False )
2302
+ return self .take (inds , axis = axis )
2246
2303
else :
2247
- return self .take (loc , axis = axis , convert = True )
2304
+ return self .take (loc , axis = axis )
2248
2305
2249
2306
if not is_scalar (loc ):
2250
2307
new_index = self .index [loc ]
@@ -5057,7 +5114,7 @@ def at_time(self, time, asof=False):
5057
5114
"""
5058
5115
try :
5059
5116
indexer = self .index .indexer_at_time (time , asof = asof )
5060
- return self .take (indexer , convert = False )
5117
+ return self .take (indexer )
5061
5118
except AttributeError :
5062
5119
raise TypeError ('Index must be DatetimeIndex' )
5063
5120
@@ -5081,7 +5138,7 @@ def between_time(self, start_time, end_time, include_start=True,
5081
5138
indexer = self .index .indexer_between_time (
5082
5139
start_time , end_time , include_start = include_start ,
5083
5140
include_end = include_end )
5084
- return self .take (indexer , convert = False )
5141
+ return self .take (indexer )
5085
5142
except AttributeError :
5086
5143
raise TypeError ('Index must be DatetimeIndex' )
5087
5144
0 commit comments