Skip to content

Commit da27bea

Browse files
gfyoungNo-Stream
authored andcommitted
DEPR: Deprecate the convert parameter completely (pandas-dev#17831)
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 pandas-devgh-17828.
1 parent b50d765 commit da27bea

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

pandas/core/generic.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -2220,6 +2220,7 @@ def _take(self, indices, axis=0, convert=True, is_copy=True):
22202220
selecting rows, "1" means that we are selecting columns, etc.
22212221
convert : bool, default True
22222222
.. deprecated:: 0.21.0
2223+
In the future, negative indices will always be converted.
22232224
22242225
Whether to convert negative indices into positive ones.
22252226
For example, ``-1`` would map to the ``len(axis) - 1``.
@@ -2282,14 +2283,15 @@ class max_speed
22822283
"""
22832284

22842285
@Appender(_shared_docs['take'])
2285-
def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs):
2286-
nv.validate_take(tuple(), kwargs)
2287-
2288-
if not convert:
2286+
def take(self, indices, axis=0, convert=None, is_copy=True, **kwargs):
2287+
if convert is not None:
22892288
msg = ("The 'convert' parameter is deprecated "
22902289
"and will be removed in a future version.")
22912290
warnings.warn(msg, FutureWarning, stacklevel=2)
2291+
else:
2292+
convert = True
22922293

2294+
convert = nv.validate_take(tuple(), kwargs)
22932295
return self._take(indices, axis=axis, convert=convert, is_copy=is_copy)
22942296

22952297
def xs(self, key, axis=0, level=None, drop_level=True):

pandas/core/sparse/series.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def _ixs(self, i, axis=0):
387387
"""
388388
label = self.index[i]
389389
if isinstance(label, Index):
390-
return self.take(i, axis=axis, convert=True)
390+
return self.take(i, axis=axis)
391391
else:
392392
return self._get_val_at(i)
393393

@@ -629,14 +629,15 @@ def sparse_reindex(self, new_index):
629629
fill_value=self.fill_value).__finalize__(self)
630630

631631
@Appender(generic._shared_docs['take'])
632-
def take(self, indices, axis=0, convert=True, *args, **kwargs):
633-
convert = nv.validate_take_with_convert(convert, args, kwargs)
634-
635-
if not convert:
632+
def take(self, indices, axis=0, convert=None, *args, **kwargs):
633+
if convert is not None:
636634
msg = ("The 'convert' parameter is deprecated "
637635
"and will be removed in a future version.")
638636
warnings.warn(msg, FutureWarning, stacklevel=2)
637+
else:
638+
convert = True
639639

640+
nv.validate_take_with_convert(convert, args, kwargs)
640641
new_values = SparseArray.take(self.values, indices)
641642
new_index = self.index.take(indices)
642643
return self._constructor(new_values,

pandas/tests/frame/test_axis_select_reindex.py

+4
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,10 @@ def test_take(self):
946946
expected = df.reindex(df.index.take(order))
947947
assert_frame_equal(result, expected)
948948

949+
with tm.assert_produces_warning(FutureWarning):
950+
result = df.take(order, convert=True, axis=0)
951+
assert_frame_equal(result, expected)
952+
949953
with tm.assert_produces_warning(FutureWarning):
950954
result = df.take(order, convert=False, axis=0)
951955
assert_frame_equal(result, expected)

pandas/tests/sparse/test_series.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -528,28 +528,27 @@ 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):
541+
if not _np_version_under1p12:
543542
tm.assert_series_equal(np.take(sp, indices, axis=0).to_dense(),
544543
np.take(sp.to_dense(), indices, axis=0))
545544

546-
msg = "the 'out' parameter is not supported"
547-
tm.assert_raises_regex(ValueError, msg, np.take,
548-
sp, indices, out=np.empty(sp.shape))
545+
msg = "the 'out' parameter is not supported"
546+
tm.assert_raises_regex(ValueError, msg, np.take,
547+
sp, indices, out=np.empty(sp.shape))
549548

550-
msg = "the 'mode' parameter is not supported"
551-
tm.assert_raises_regex(ValueError, msg, np.take,
552-
sp, indices, mode='clip')
549+
msg = "the 'mode' parameter is not supported"
550+
tm.assert_raises_regex(ValueError, msg, np.take,
551+
sp, indices, out=None, mode='clip')
553552

554553
def test_setitem(self):
555554
self.bseries[5] = 7.

0 commit comments

Comments
 (0)