Skip to content

Commit d5337d1

Browse files
committed
DEPR: Deprecate the convert parameter completely
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.
1 parent 0f548d4 commit d5337d1

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

pandas/compat/numpy/function.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,11 @@ def validate_take_with_convert(convert, args, kwargs):
247247
"""
248248
If this function is called via the 'numpy' library, the third
249249
parameter in its signature is 'axis', which takes either an
250-
ndarray or 'None', so check if the 'convert' parameter is either
251-
an instance of ndarray or is None
250+
ndarray or 'None'. We don't mind if 'convert' is 'None', but we
251+
should check if it is an instance of ndarray.
252252
"""
253253

254-
if isinstance(convert, ndarray) or convert is None:
254+
if isinstance(convert, ndarray):
255255
args = (convert,) + args
256256
convert = True
257257

pandas/core/generic.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -2135,13 +2135,18 @@ def _take(self, indices, axis=0, convert=True, is_copy=True):
21352135
axis : int, default 0
21362136
The axis on which to select elements. "0" means that we are
21372137
selecting rows, "1" means that we are selecting columns, etc.
2138-
convert : bool, default True
2138+
convert : bool, default None
21392139
.. deprecated:: 0.21.0
2140+
In the future, negative indices will always be converted.
21402141
21412142
Whether to convert negative indices into positive ones.
21422143
For example, ``-1`` would map to the ``len(axis) - 1``.
21432144
The conversions are similar to the behavior of indexing a
21442145
regular Python list.
2146+
2147+
If ``None`` is passed in, negative indices will be converted
2148+
to positive indices.
2149+
21452150
is_copy : bool, default True
21462151
Whether to return a copy of the original object or not.
21472152
@@ -2199,13 +2204,15 @@ class max_speed
21992204
"""
22002205

22012206
@Appender(_shared_docs['take'])
2202-
def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs):
2207+
def take(self, indices, axis=0, convert=None, is_copy=True, **kwargs):
22032208
nv.validate_take(tuple(), kwargs)
22042209

2205-
if not convert:
2210+
if convert is not None:
22062211
msg = ("The 'convert' parameter is deprecated "
22072212
"and will be removed in a future version.")
22082213
warnings.warn(msg, FutureWarning, stacklevel=2)
2214+
else:
2215+
convert = True
22092216

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

pandas/core/sparse/series.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def _ixs(self, i, axis=0):
386386
"""
387387
label = self.index[i]
388388
if isinstance(label, Index):
389-
return self.take(i, axis=axis, convert=True)
389+
return self.take(i, axis=axis)
390390
else:
391391
return self._get_val_at(i)
392392

@@ -628,10 +628,10 @@ def sparse_reindex(self, new_index):
628628
fill_value=self.fill_value).__finalize__(self)
629629

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

634-
if not convert:
634+
if convert is not None:
635635
msg = ("The 'convert' parameter is deprecated "
636636
"and will be removed in a future version.")
637637
warnings.warn(msg, FutureWarning, stacklevel=2)

pandas/tests/frame/test_axis_select_reindex.py

+4
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,10 @@ def test_take(self):
852852
expected = df.reindex(df.index.take(order))
853853
assert_frame_equal(result, expected)
854854

855+
with tm.assert_produces_warning(FutureWarning):
856+
result = df.take(order, convert=False, axis=0)
857+
assert_frame_equal(result, expected)
858+
855859
with tm.assert_produces_warning(FutureWarning):
856860
result = df.take(order, convert=False, axis=0)
857861
assert_frame_equal(result, expected)

pandas/tests/sparse/test_series.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import pandas as pd
1111

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

531+
with tm.assert_produces_warning(FutureWarning):
532+
sp.take([1, 5], convert=True)
533+
531534
with tm.assert_produces_warning(FutureWarning):
532535
sp.take([1, 5], convert=False)
533536

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

538-
# gh-17352: older versions of numpy don't properly
539-
# pass in arguments to downstream .take() implementations.
540-
warning = FutureWarning if _np_version_under1p12 else None
541-
542-
with tm.assert_produces_warning(warning, check_stacklevel=False):
543-
tm.assert_series_equal(np.take(sp, indices, axis=0).to_dense(),
544-
np.take(sp.to_dense(), indices, axis=0))
541+
tm.assert_series_equal(np.take(sp, indices, axis=0).to_dense(),
542+
np.take(sp.to_dense(), indices, axis=0))
545543

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

0 commit comments

Comments
 (0)