diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 7a2bd2708b711..ffb124af4f5fc 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2641,7 +2641,7 @@ def _ixs(self, i, axis=0): return self.loc[:, lab_slice] else: if isinstance(label, Index): - return self._take(i, axis=1, convert=True) + return self._take(i, axis=1) index_len = len(self.index) @@ -2720,10 +2720,10 @@ def _getitem_array(self, key): # be reindexed to match DataFrame rows key = check_bool_indexer(self.index, key) indexer = key.nonzero()[0] - return self._take(indexer, axis=0, convert=False) + return self._take(indexer, axis=0) else: indexer = self.loc._convert_to_indexer(key, axis=1) - return self._take(indexer, axis=1, convert=True) + return self._take(indexer, axis=1) def _getitem_multilevel(self, key): loc = self.columns.get_loc(key) @@ -4292,7 +4292,7 @@ def dropna(self, axis=0, how='any', thresh=None, subset=None, else: raise TypeError('must specify how or thresh') - result = self._take(mask.nonzero()[0], axis=axis, convert=False) + result = self._take(mask.nonzero()[0], axis=axis) if inplace: self._update_inplace(result) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 96340f9e82992..e96a2a9f08520 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -37,7 +37,6 @@ from pandas.core.index import (Index, MultiIndex, _ensure_index, InvalidIndexError, RangeIndex) import pandas.core.indexing as indexing -from pandas.core.indexing import maybe_convert_indices from pandas.core.indexes.datetimes import DatetimeIndex from pandas.core.indexes.period import PeriodIndex, Period from pandas.core.internals import BlockManager @@ -2510,8 +2509,7 @@ def _iget_item_cache(self, item): if ax.is_unique: lower = self._get_item_cache(ax[item]) else: - lower = self._take(item, axis=self._info_axis_number, - convert=True) + lower = self._take(item, axis=self._info_axis_number) return lower def _box_item_values(self, key, values): @@ -2765,11 +2763,6 @@ def __delitem__(self, key): axis : int, default 0 The axis on which to select elements. "0" means that we are selecting rows, "1" means that we are selecting columns, etc. - convert : bool, default True - Whether to convert negative indices into positive ones. - For example, ``-1`` would map to the ``len(axis) - 1``. - The conversions are similar to the behavior of indexing a - regular Python list. is_copy : bool, default True Whether to return a copy of the original object or not. @@ -2785,12 +2778,9 @@ def __delitem__(self, key): """ @Appender(_shared_docs['_take']) - def _take(self, indices, axis=0, convert=True, is_copy=True): + def _take(self, indices, axis=0, is_copy=True): self._consolidate_inplace() - if convert: - indices = maybe_convert_indices(indices, len(self._get_axis(axis))) - new_data = self._data.take(indices, axis=self._get_block_manager_axis(axis), verify=True) @@ -2893,11 +2883,9 @@ def take(self, indices, axis=0, convert=None, is_copy=True, **kwargs): msg = ("The 'convert' parameter is deprecated " "and will be removed in a future version.") warnings.warn(msg, FutureWarning, stacklevel=2) - else: - convert = True - convert = nv.validate_take(tuple(), kwargs) - return self._take(indices, axis=axis, convert=convert, is_copy=is_copy) + nv.validate_take(tuple(), kwargs) + return self._take(indices, axis=axis, is_copy=is_copy) def xs(self, key, axis=0, level=None, drop_level=True): """ @@ -2998,9 +2986,9 @@ def xs(self, key, axis=0, level=None, drop_level=True): if isinstance(loc, np.ndarray): if loc.dtype == np.bool_: inds, = loc.nonzero() - return self._take(inds, axis=axis, convert=False) + return self._take(inds, axis=axis) else: - return self._take(loc, axis=axis, convert=True) + return self._take(loc, axis=axis) if not is_scalar(loc): new_index = self.index[loc] @@ -6784,7 +6772,7 @@ def at_time(self, time, asof=False): """ try: indexer = self.index.indexer_at_time(time, asof=asof) - return self._take(indexer, convert=False) + return self._take(indexer) except AttributeError: raise TypeError('Index must be DatetimeIndex') @@ -6808,7 +6796,7 @@ def between_time(self, start_time, end_time, include_start=True, indexer = self.index.indexer_between_time( start_time, end_time, include_start=include_start, include_end=include_end) - return self._take(indexer, convert=False) + return self._take(indexer) except AttributeError: raise TypeError('Index must be DatetimeIndex') diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 2fe1f303236bb..f78f7cb625218 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -508,8 +508,7 @@ def _set_grouper(self, obj, sort=False): # use stable sort to support first, last, nth indexer = self.indexer = ax.argsort(kind='mergesort') ax = ax.take(indexer) - obj = obj._take(indexer, axis=self.axis, - convert=False, is_copy=False) + obj = obj._take(indexer, axis=self.axis, is_copy=False) self.obj = obj self.grouper = ax @@ -860,7 +859,7 @@ def get_group(self, name, obj=None): if not len(inds): raise KeyError(name) - return obj._take(inds, axis=self.axis, convert=False) + return obj._take(inds, axis=self.axis) def __iter__(self): """ @@ -1437,9 +1436,9 @@ def last(x): cls.min = groupby_function('min', 'min', np.min, numeric_only=False) cls.max = groupby_function('max', 'max', np.max, numeric_only=False) cls.first = groupby_function('first', 'first', first_compat, - numeric_only=False, _convert=True) + numeric_only=False) cls.last = groupby_function('last', 'last', last_compat, - numeric_only=False, _convert=True) + numeric_only=False) @Substitution(name='groupby') @Appender(_doc_template) @@ -2653,7 +2652,7 @@ def _aggregate_series_fast(self, obj, func): # avoids object / Series creation overhead dummy = obj._get_values(slice(None, 0)).to_dense() indexer = get_group_index_sorter(group_index, ngroups) - obj = obj._take(indexer, convert=False).to_dense() + obj = obj._take(indexer).to_dense() group_index = algorithms.take_nd( group_index, indexer, allow_fill=False) grouper = reduction.SeriesGrouper(obj, func, group_index, ngroups, @@ -5032,7 +5031,7 @@ def __iter__(self): yield i, self._chop(sdata, slice(start, end)) def _get_sorted_data(self): - return self.data._take(self.sort_idx, axis=self.axis, convert=False) + return self.data._take(self.sort_idx, axis=self.axis) def _chop(self, sdata, slice_obj): return sdata.iloc[slice_obj] diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index d23beba1c534d..7a7e47803c240 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1126,7 +1126,7 @@ def _getitem_iterable(self, key, axis=None): if com.is_bool_indexer(key): key = check_bool_indexer(labels, key) inds, = key.nonzero() - return self.obj._take(inds, axis=axis, convert=False) + return self.obj._take(inds, axis=axis) else: # Have the index compute an indexer or return None # if it cannot handle; we only act on all found values @@ -1158,8 +1158,7 @@ def _getitem_iterable(self, key, axis=None): keyarr) if new_indexer is not None: - result = self.obj._take(indexer[indexer != -1], axis=axis, - convert=False) + result = self.obj._take(indexer[indexer != -1], axis=axis) self._validate_read_indexer(key, new_indexer, axis) result = result._reindex_with_indexers( @@ -1356,7 +1355,7 @@ def _get_slice_axis(self, slice_obj, axis=None): if isinstance(indexer, slice): return self._slice(indexer, axis=axis, kind='iloc') else: - return self.obj._take(indexer, axis=axis, convert=False) + return self.obj._take(indexer, axis=axis) class _IXIndexer(_NDFrameIndexer): @@ -1494,7 +1493,7 @@ def _getbool_axis(self, key, axis=None): key = check_bool_indexer(labels, key) inds, = key.nonzero() try: - return self.obj._take(inds, axis=axis, convert=False) + return self.obj._take(inds, axis=axis) except Exception as detail: raise self._exception(detail) @@ -1514,7 +1513,7 @@ def _get_slice_axis(self, slice_obj, axis=None): if isinstance(indexer, slice): return self._slice(indexer, axis=axis, kind='iloc') else: - return self.obj._take(indexer, axis=axis, convert=False) + return self.obj._take(indexer, axis=axis) class _LocIndexer(_LocationIndexer): @@ -2050,7 +2049,7 @@ def _get_slice_axis(self, slice_obj, axis=None): if isinstance(slice_obj, slice): return self._slice(slice_obj, axis=axis, kind='iloc') else: - return self.obj._take(slice_obj, axis=axis, convert=False) + return self.obj._take(slice_obj, axis=axis) def _get_list_axis(self, key, axis=None): """ @@ -2068,7 +2067,7 @@ def _get_list_axis(self, key, axis=None): if axis is None: axis = self.axis or 0 try: - return self.obj._take(key, axis=axis, convert=False) + return self.obj._take(key, axis=axis) except IndexError: # re-raise with different error message raise IndexError("positional indexers are out-of-bounds") diff --git a/pandas/core/series.py b/pandas/core/series.py index 951227f381b1c..0e2ae22f35af7 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -3503,9 +3503,7 @@ def memory_usage(self, index=True, deep=False): return v @Appender(generic._shared_docs['_take']) - def _take(self, indices, axis=0, convert=True, is_copy=False): - if convert: - indices = maybe_convert_indices(indices, len(self._get_axis(axis))) + def _take(self, indices, axis=0, is_copy=False): indices = _ensure_platform_int(indices) new_index = self.index.take(indices) @@ -3513,6 +3511,7 @@ def _take(self, indices, axis=0, convert=True, is_copy=False): if is_categorical_dtype(self): # https://github.com/pandas-dev/pandas/issues/20664 # TODO: remove when the default Categorical.take behavior changes + indices = maybe_convert_indices(indices, len(self._get_axis(axis))) kwargs = {'allow_fill': False} else: kwargs = {}