Skip to content

Commit

Permalink
DEPR: Deprecate the convert parameter completely
Browse files Browse the repository at this point in the history
Previously, we weren't issuing a warning if the user
happened to pass in the original default of "True",
which would cause downstream code to break.

Closes gh-17828.
  • Loading branch information
gfyoung committed Oct 10, 2017
1 parent 0f548d4 commit d5337d1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
6 changes: 3 additions & 3 deletions pandas/compat/numpy/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ def validate_take_with_convert(convert, args, kwargs):
"""
If this function is called via the 'numpy' library, the third
parameter in its signature is 'axis', which takes either an
ndarray or 'None', so check if the 'convert' parameter is either
an instance of ndarray or is None
ndarray or 'None'. We don't mind if 'convert' is 'None', but we
should check if it is an instance of ndarray.
"""

if isinstance(convert, ndarray) or convert is None:
if isinstance(convert, ndarray):
args = (convert,) + args
convert = True

Expand Down
13 changes: 10 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2135,13 +2135,18 @@ def _take(self, indices, axis=0, convert=True, is_copy=True):
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
convert : bool, default None
.. deprecated:: 0.21.0
In the future, negative indices will always be converted.
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.
If ``None`` is passed in, negative indices will be converted
to positive indices.
is_copy : bool, default True
Whether to return a copy of the original object or not.
Expand Down Expand Up @@ -2199,13 +2204,15 @@ class max_speed
"""

@Appender(_shared_docs['take'])
def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs):
def take(self, indices, axis=0, convert=None, is_copy=True, **kwargs):
nv.validate_take(tuple(), kwargs)

if not convert:
if convert is not None:
msg = ("The 'convert' parameter is deprecated "
"and will be removed in a future version.")
warnings.warn(msg, FutureWarning, stacklevel=2)
else:
convert = True

return self._take(indices, axis=axis, convert=convert, is_copy=is_copy)

Expand Down
8 changes: 4 additions & 4 deletions pandas/core/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def _ixs(self, i, axis=0):
"""
label = self.index[i]
if isinstance(label, Index):
return self.take(i, axis=axis, convert=True)
return self.take(i, axis=axis)
else:
return self._get_val_at(i)

Expand Down Expand Up @@ -628,10 +628,10 @@ def sparse_reindex(self, new_index):
fill_value=self.fill_value).__finalize__(self)

@Appender(generic._shared_docs['take'])
def take(self, indices, axis=0, convert=True, *args, **kwargs):
convert = nv.validate_take_with_convert(convert, args, kwargs)
def take(self, indices, axis=0, convert=None, *args, **kwargs):
nv.validate_take_with_convert(convert, args, kwargs)

if not convert:
if convert is not None:
msg = ("The 'convert' parameter is deprecated "
"and will be removed in a future version.")
warnings.warn(msg, FutureWarning, stacklevel=2)
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/frame/test_axis_select_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,10 @@ def test_take(self):
expected = df.reindex(df.index.take(order))
assert_frame_equal(result, expected)

with tm.assert_produces_warning(FutureWarning):
result = df.take(order, convert=False, axis=0)
assert_frame_equal(result, expected)

with tm.assert_produces_warning(FutureWarning):
result = df.take(order, convert=False, axis=0)
assert_frame_equal(result, expected)
Expand Down
14 changes: 6 additions & 8 deletions pandas/tests/sparse/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pandas as pd

from pandas import (Series, DataFrame, bdate_range,
isna, compat, _np_version_under1p12)
isna, compat)
from pandas.tseries.offsets import BDay
import pandas.util.testing as tm
from pandas.compat import range
Expand Down Expand Up @@ -528,20 +528,18 @@ def _compare(idx):
exp = pd.Series(np.repeat(nan, 5))
tm.assert_series_equal(sp.take([0, 1, 2, 3, 4]), exp)

with tm.assert_produces_warning(FutureWarning):
sp.take([1, 5], convert=True)

with tm.assert_produces_warning(FutureWarning):
sp.take([1, 5], convert=False)

def test_numpy_take(self):
sp = SparseSeries([1.0, 2.0, 3.0])
indices = [1, 2]

# gh-17352: older versions of numpy don't properly
# pass in arguments to downstream .take() implementations.
warning = FutureWarning if _np_version_under1p12 else None

with tm.assert_produces_warning(warning, check_stacklevel=False):
tm.assert_series_equal(np.take(sp, indices, axis=0).to_dense(),
np.take(sp.to_dense(), indices, axis=0))
tm.assert_series_equal(np.take(sp, indices, axis=0).to_dense(),
np.take(sp.to_dense(), indices, axis=0))

msg = "the 'out' parameter is not supported"
tm.assert_raises_regex(ValueError, msg, np.take,
Expand Down

0 comments on commit d5337d1

Please sign in to comment.