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
@@ -1822,7 +1823,8 @@ def _iget_item_cache(self, item):
18221823 if ax .is_unique :
18231824 lower = self ._get_item_cache (ax [item ])
18241825 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 )
18261828 return lower
18271829
18281830 def _box_item_values (self , key , values ):
@@ -2057,8 +2059,63 @@ def __delitem__(self, key):
20572059 except KeyError :
20582060 pass
20592061
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
20612097 """
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' ] = """
20622119 Return the elements in the given *positional* indices along an axis.
20632120
20642121 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):
20732130 The axis on which to select elements. "0" means that we are
20742131 selecting rows, "1" means that we are selecting columns, etc.
20752132 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.
20792139 is_copy : bool, default True
20802140 Whether to return a copy of the original object or not.
20812141
@@ -2131,19 +2191,17 @@ class max_speed
21312191 numpy.ndarray.take
21322192 numpy.take
21332193 """
2194+
2195+ @Appender (_shared_docs ['take' ])
2196+ def take (self , indices , axis = 0 , convert = True , is_copy = True , ** kwargs ):
21342197 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 )
21402198
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 )
21452203
2146- return result
2204+ return self . _take ( indices , axis = axis , convert = convert , is_copy = is_copy )
21472205
21482206 def xs (self , key , axis = 0 , level = None , drop_level = True ):
21492207 """
@@ -2244,9 +2302,9 @@ def xs(self, key, axis=0, level=None, drop_level=True):
22442302 if isinstance (loc , np .ndarray ):
22452303 if loc .dtype == np .bool_ :
22462304 inds , = loc .nonzero ()
2247- return self .take (inds , axis = axis , convert = False )
2305+ return self ._take (inds , axis = axis , convert = False )
22482306 else :
2249- return self .take (loc , axis = axis , convert = True )
2307+ return self ._take (loc , axis = axis , convert = True )
22502308
22512309 if not is_scalar (loc ):
22522310 new_index = self .index [loc ]
@@ -5112,7 +5170,7 @@ def at_time(self, time, asof=False):
51125170 """
51135171 try :
51145172 indexer = self .index .indexer_at_time (time , asof = asof )
5115- return self .take (indexer , convert = False )
5173+ return self ._take (indexer , convert = False )
51165174 except AttributeError :
51175175 raise TypeError ('Index must be DatetimeIndex' )
51185176
@@ -5136,7 +5194,7 @@ def between_time(self, start_time, end_time, include_start=True,
51365194 indexer = self .index .indexer_between_time (
51375195 start_time , end_time , include_start = include_start ,
51385196 include_end = include_end )
5139- return self .take (indexer , convert = False )
5197+ return self ._take (indexer , convert = False )
51405198 except AttributeError :
51415199 raise TypeError ('Index must be DatetimeIndex' )
51425200
0 commit comments