-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DEPR: Deprecate convert parameter in take #17352
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2034,7 +2034,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, convert=True) | ||
|
||
index_len = len(self.index) | ||
|
||
|
@@ -2116,10 +2116,10 @@ def _getitem_array(self, key): | |
# be reindexed to match DataFrame rows | ||
key = check_bool_indexer(self.index, key) | ||
indexer = key.nonzero()[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still more places to change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
return self.take(indexer, axis=0, convert=False) | ||
return self._take(indexer, axis=0, convert=False) | ||
else: | ||
indexer = self.loc._convert_to_indexer(key, axis=1) | ||
return self.take(indexer, axis=1, convert=True) | ||
return self._take(indexer, axis=1, convert=True) | ||
|
||
def _getitem_multilevel(self, key): | ||
loc = self.columns.get_loc(key) | ||
|
@@ -3355,7 +3355,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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think these internal uses should be removed. We're guaranteed the indexer is inbounds, so it's an unnecessary performance hit to check it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, this example doesn't matter b/c it's on a frame, but does with the usages of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there's a bounds check anymore in |
||
result = self._take(mask.nonzero()[0], axis=axis, convert=False) | ||
|
||
if inplace: | ||
self._update_inplace(result) | ||
|
@@ -3486,7 +3486,7 @@ def sort_values(self, by, axis=0, ascending=True, inplace=False, | |
|
||
new_data = self._data.take(indexer, | ||
axis=self._get_block_manager_axis(axis), | ||
convert=False, verify=False) | ||
verify=False) | ||
|
||
if inplace: | ||
return self._update_inplace(new_data) | ||
|
@@ -3547,7 +3547,7 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False, | |
baxis = self._get_block_manager_axis(axis) | ||
new_data = self._data.take(indexer, | ||
axis=baxis, | ||
convert=False, verify=False) | ||
verify=False) | ||
|
||
# reconstruct axis if needed | ||
new_data.axes[baxis] = new_data.axes[baxis]._sort_levels_monotonic() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
from pandas.core.index import (Index, MultiIndex, _ensure_index, | ||
InvalidIndexError) | ||
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 | ||
|
@@ -1822,7 +1823,8 @@ 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, | ||
convert=True) | ||
return lower | ||
|
||
def _box_item_values(self, key, values): | ||
|
@@ -2057,8 +2059,63 @@ def __delitem__(self, key): | |
except KeyError: | ||
pass | ||
|
||
def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs): | ||
_shared_docs['_take'] = """ | ||
Return the elements in the given *positional* indices along an axis. | ||
|
||
This means that we are not indexing according to actual values in | ||
the index attribute of the object. We are indexing according to the | ||
actual position of the element in the object. | ||
|
||
This is the internal version of ``.take()`` and will contain a wider | ||
selection of parameters useful for internal use but not as suitable | ||
for public usage. | ||
|
||
Parameters | ||
---------- | ||
indices : array-like | ||
An array of ints indicating which positions to take. | ||
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. | ||
|
||
Returns | ||
------- | ||
taken : type of caller | ||
An array-like containing the elements taken from the object. | ||
|
||
See Also | ||
-------- | ||
numpy.ndarray.take | ||
numpy.take | ||
""" | ||
|
||
@Appender(_shared_docs['_take']) | ||
def _take(self, indices, axis=0, convert=True, 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) | ||
result = self._constructor(new_data).__finalize__(self) | ||
|
||
# Maybe set copy if we didn't actually change the index. | ||
if is_copy: | ||
if not result._get_axis(axis).equals(self._get_axis(axis)): | ||
result._set_is_copy(self) | ||
|
||
return result | ||
|
||
_shared_docs['take'] = """ | ||
Return the elements in the given *positional* indices along an axis. | ||
|
||
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): | |
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 to positive ones, just as with | ||
indexing into Python lists. For example, if `-1` was passed in, | ||
this index would be converted ``n - 1``. | ||
.. deprecated:: 0.21.0 | ||
|
||
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. | ||
|
||
|
@@ -2131,19 +2191,17 @@ class max_speed | |
numpy.ndarray.take | ||
numpy.take | ||
""" | ||
|
||
@Appender(_shared_docs['take']) | ||
def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs): | ||
nv.validate_take(tuple(), kwargs) | ||
self._consolidate_inplace() | ||
new_data = self._data.take(indices, | ||
axis=self._get_block_manager_axis(axis), | ||
convert=True, verify=True) | ||
result = self._constructor(new_data).__finalize__(self) | ||
|
||
# maybe set copy if we didn't actually change the index | ||
if is_copy: | ||
if not result._get_axis(axis).equals(self._get_axis(axis)): | ||
result._set_is_copy(self) | ||
if not convert: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why!? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the point is to deprecate this. How can you tell that someone is then passing the convert parameter at all? you can then check if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's because |
||
msg = ("The 'convert' parameter is deprecated " | ||
"and will be removed in a future version.") | ||
warnings.warn(msg, FutureWarning, stacklevel=2) | ||
|
||
return result | ||
return self._take(indices, axis=axis, convert=convert, is_copy=is_copy) | ||
|
||
def xs(self, key, axis=0, level=None, drop_level=True): | ||
""" | ||
|
@@ -2244,9 +2302,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, convert=False) | ||
else: | ||
return self.take(loc, axis=axis, convert=True) | ||
return self._take(loc, axis=axis, convert=True) | ||
|
||
if not is_scalar(loc): | ||
new_index = self.index[loc] | ||
|
@@ -5112,7 +5170,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, convert=False) | ||
except AttributeError: | ||
raise TypeError('Index must be DatetimeIndex') | ||
|
||
|
@@ -5136,7 +5194,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, convert=False) | ||
except AttributeError: | ||
raise TypeError('Index must be DatetimeIndex') | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2563,35 +2563,24 @@ def memory_usage(self, index=True, deep=False): | |
v += self.index.memory_usage(deep=deep) | ||
return v | ||
|
||
def take(self, indices, axis=0, convert=True, is_copy=False, **kwargs): | ||
""" | ||
return Series corresponding to requested indices | ||
|
||
Parameters | ||
---------- | ||
indices : list / array of ints | ||
convert : translate negative to positive indices (default) | ||
|
||
Returns | ||
------- | ||
taken : Series | ||
|
||
See also | ||
-------- | ||
numpy.ndarray.take | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we have this? now that it is generic is shouldn't be needed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have what exactly? You commented on a not-so clear part of the diff. |
||
""" | ||
if kwargs: | ||
nv.validate_take(tuple(), kwargs) | ||
|
||
# check/convert indicies here | ||
@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))) | ||
|
||
indices = _ensure_platform_int(indices) | ||
new_index = self.index.take(indices) | ||
new_values = self._values.take(indices) | ||
return (self._constructor(new_values, index=new_index, fastpath=True) | ||
.__finalize__(self)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason you can't just call the super method here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no the entire routine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://travis-ci.org/pandas-dev/pandas/builds/279118227 I gave it a try, and there were a bunch of failures. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, not really sure why it shoulnt' work. its almost the same code |
||
|
||
result = (self._constructor(new_values, index=new_index, | ||
fastpath=True).__finalize__(self)) | ||
|
||
# Maybe set copy if we didn't actually change the index. | ||
if is_copy: | ||
if not result._get_axis(axis).equals(self._get_axis(axis)): | ||
result._set_is_copy(self) | ||
|
||
return result | ||
|
||
def isin(self, values): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -602,16 +602,15 @@ def sparse_reindex(self, new_index): | |
sparse_index=new_index, | ||
fill_value=self.fill_value).__finalize__(self) | ||
|
||
@Appender(generic._shared_docs['take']) | ||
def take(self, indices, axis=0, convert=True, *args, **kwargs): | ||
""" | ||
Sparse-compatible version of ndarray.take | ||
convert = nv.validate_take_with_convert(convert, args, kwargs) | ||
|
||
Returns | ||
------- | ||
taken : ndarray | ||
""" | ||
if not convert: | ||
msg = ("The 'convert' parameter is deprecated " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same answer as above. |
||
"and will be removed in a future version.") | ||
warnings.warn(msg, FutureWarning, stacklevel=2) | ||
|
||
convert = nv.validate_take_with_convert(convert, args, kwargs) | ||
new_values = SparseArray.take(self.values, indices) | ||
new_index = self.index.take(indices) | ||
return self._constructor(new_values, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you change the rest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Working on them.