3838from pandas .core .index import (Index , MultiIndex , _ensure_index ,
3939 InvalidIndexError )
4040import pandas .core .indexing as indexing
41+ from pandas .core .indexing import maybe_convert_indices
4142from pandas .core .indexes .datetimes import DatetimeIndex
4243from pandas .core .indexes .period import PeriodIndex , Period
4344from pandas .core .internals import BlockManager
@@ -1820,7 +1821,7 @@ def _iget_item_cache(self, item):
18201821 if ax .is_unique :
18211822 lower = self ._get_item_cache (ax [item ])
18221823 else :
1823- lower = self .take (item , axis = self ._info_axis_number , convert = True )
1824+ lower = self .take (item , axis = self ._info_axis_number )
18241825 return lower
18251826
18261827 def _box_item_values (self , key , values ):
@@ -2055,8 +2056,63 @@ def __delitem__(self, key):
20552056 except KeyError :
20562057 pass
20572058
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
20592094 """
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' ] = """
20602116 Return the elements in the given *positional* indices along an axis.
20612117
20622118 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):
20712127 The axis on which to select elements. "0" means that we are
20722128 selecting rows, "1" means that we are selecting columns, etc.
20732129 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.
20772136 is_copy : bool, default True
20782137 Whether to return a copy of the original object or not.
20792138
@@ -2129,19 +2188,17 @@ class max_speed
21292188 numpy.ndarray.take
21302189 numpy.take
21312190 """
2191+
2192+ @Appender (_shared_docs ['take' ])
2193+ def take (self , indices , axis = 0 , convert = True , is_copy = True , ** kwargs ):
21322194 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 )
21382195
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 )
21432200
2144- return result
2201+ return self . _take ( indices , axis = axis , convert = convert , is_copy = is_copy )
21452202
21462203 def xs (self , key , axis = 0 , level = None , drop_level = True ):
21472204 """
@@ -2242,9 +2299,9 @@ def xs(self, key, axis=0, level=None, drop_level=True):
22422299 if isinstance (loc , np .ndarray ):
22432300 if loc .dtype == np .bool_ :
22442301 inds , = loc .nonzero ()
2245- return self .take (inds , axis = axis , convert = False )
2302+ return self .take (inds , axis = axis )
22462303 else :
2247- return self .take (loc , axis = axis , convert = True )
2304+ return self .take (loc , axis = axis )
22482305
22492306 if not is_scalar (loc ):
22502307 new_index = self .index [loc ]
@@ -5057,7 +5114,7 @@ def at_time(self, time, asof=False):
50575114 """
50585115 try :
50595116 indexer = self .index .indexer_at_time (time , asof = asof )
5060- return self .take (indexer , convert = False )
5117+ return self .take (indexer )
50615118 except AttributeError :
50625119 raise TypeError ('Index must be DatetimeIndex' )
50635120
@@ -5081,7 +5138,7 @@ def between_time(self, start_time, end_time, include_start=True,
50815138 indexer = self .index .indexer_between_time (
50825139 start_time , end_time , include_start = include_start ,
50835140 include_end = include_end )
5084- return self .take (indexer , convert = False )
5141+ return self .take (indexer )
50855142 except AttributeError :
50865143 raise TypeError ('Index must be DatetimeIndex' )
50875144
0 commit comments