Skip to content

Commit 5ac5e38

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 727ea20 commit 5ac5e38

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

pandas/core/generic.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -2172,6 +2172,7 @@ def _take(self, indices, axis=0, convert=True, is_copy=True):
21722172
selecting rows, "1" means that we are selecting columns, etc.
21732173
convert : bool, default True
21742174
.. deprecated:: 0.21.0
2175+
In the future, negative indices will always be converted.
21752176
21762177
Whether to convert negative indices into positive ones.
21772178
For example, ``-1`` would map to the ``len(axis) - 1``.
@@ -2234,14 +2235,15 @@ class max_speed
22342235
"""
22352236

22362237
@Appender(_shared_docs['take'])
2237-
def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs):
2238-
nv.validate_take(tuple(), kwargs)
2239-
2240-
if not convert:
2238+
def take(self, indices, axis=0, convert=None, is_copy=True, **kwargs):
2239+
if convert is not None:
22412240
msg = ("The 'convert' parameter is deprecated "
22422241
"and will be removed in a future version.")
22432242
warnings.warn(msg, FutureWarning, stacklevel=2)
2243+
else:
2244+
convert = True
22442245

2246+
convert = nv.validate_take(tuple(), kwargs)
22452247
return self._take(indices, axis=axis, convert=convert, is_copy=is_copy)
22462248

22472249
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
@@ -944,6 +944,10 @@ def test_take(self):
944944
expected = df.reindex(df.index.take(order))
945945
assert_frame_equal(result, expected)
946946

947+
with tm.assert_produces_warning(FutureWarning):
948+
result = df.take(order, convert=True, axis=0)
949+
assert_frame_equal(result, expected)
950+
947951
with tm.assert_produces_warning(FutureWarning):
948952
result = df.take(order, convert=False, axis=0)
949953
assert_frame_equal(result, expected)

pandas/tests/sparse/test_series.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -528,18 +528,17 @@ 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

0 commit comments

Comments
 (0)