Skip to content

Commit 78f0c4e

Browse files
committed
DEPR: Deprecate convert parameter in take
xref gh-16948. The parameter is not respected, nor is it a parameter in many 'take' implementations.
1 parent 473a7f3 commit 78f0c4e

File tree

11 files changed

+112
-42
lines changed

11 files changed

+112
-42
lines changed

doc/source/whatsnew/v0.21.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ Other API Changes
300300
Deprecations
301301
~~~~~~~~~~~~
302302
- :func:`read_excel()` has deprecated ``sheetname`` in favor of ``sheet_name`` for consistency with ``.to_excel()`` (:issue:`10559`).
303-
303+
- The ``convert`` parameter has been deprecated in the ``.take()`` method, as it was not being respected (:issue:`16948`)
304304
- ``pd.options.html.border`` has been deprecated in favor of ``pd.options.display.html.border`` (:issue:`15793`).
305305

306306
.. _whatsnew_0210.prior_deprecations:

pandas/core/frame.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -2033,7 +2033,7 @@ def _ixs(self, i, axis=0):
20332033
return self.loc[:, lab_slice]
20342034
else:
20352035
if isinstance(label, Index):
2036-
return self.take(i, axis=1, convert=True)
2036+
return self.take(i, axis=1)
20372037

20382038
index_len = len(self.index)
20392039

@@ -2115,10 +2115,10 @@ def _getitem_array(self, key):
21152115
# be reindexed to match DataFrame rows
21162116
key = check_bool_indexer(self.index, key)
21172117
indexer = key.nonzero()[0]
2118-
return self.take(indexer, axis=0, convert=False)
2118+
return self.take(indexer, axis=0)
21192119
else:
21202120
indexer = self.loc._convert_to_indexer(key, axis=1)
2121-
return self.take(indexer, axis=1, convert=True)
2121+
return self.take(indexer, axis=1)
21222122

21232123
def _getitem_multilevel(self, key):
21242124
loc = self.columns.get_loc(key)
@@ -3354,7 +3354,7 @@ def dropna(self, axis=0, how='any', thresh=None, subset=None,
33543354
else:
33553355
raise TypeError('must specify how or thresh')
33563356

3357-
result = self.take(mask.nonzero()[0], axis=axis, convert=False)
3357+
result = self.take(mask.nonzero()[0], axis=axis)
33583358

33593359
if inplace:
33603360
self._update_inplace(result)
@@ -3490,7 +3490,7 @@ def trans(v):
34903490

34913491
new_data = self._data.take(indexer,
34923492
axis=self._get_block_manager_axis(axis),
3493-
convert=False, verify=False)
3493+
verify=False)
34943494

34953495
if inplace:
34963496
return self._update_inplace(new_data)
@@ -3551,7 +3551,7 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
35513551
baxis = self._get_block_manager_axis(axis)
35523552
new_data = self._data.take(indexer,
35533553
axis=baxis,
3554-
convert=False, verify=False)
3554+
verify=False)
35553555

35563556
# reconstruct axis if needed
35573557
new_data.axes[baxis] = new_data.axes[baxis]._sort_levels_monotonic()

pandas/core/generic.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -1819,7 +1819,7 @@ def _iget_item_cache(self, item):
18191819
if ax.is_unique:
18201820
lower = self._get_item_cache(ax[item])
18211821
else:
1822-
lower = self.take(item, axis=self._info_axis_number, convert=True)
1822+
lower = self.take(item, axis=self._info_axis_number)
18231823
return lower
18241824

18251825
def _box_item_values(self, key, values):
@@ -2074,9 +2074,9 @@ def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs):
20742074
The axis on which to select elements. "0" means that we are
20752075
selecting rows, "1" means that we are selecting columns, etc.
20762076
convert : bool, default True
2077-
Whether to convert negative indices to positive ones, just as with
2078-
indexing into Python lists. For example, if `-1` was passed in,
2079-
this index would be converted ``n - 1``.
2077+
.. deprecated:: 0.21.0
2078+
2079+
This parameter is not respected by the function.
20802080
is_copy : bool, default True
20812081
Whether to return a copy of the original object or not.
20822082
@@ -2134,9 +2134,15 @@ class max_speed
21342134
"""
21352135
nv.validate_take(tuple(), kwargs)
21362136
self._consolidate_inplace()
2137+
2138+
if not convert:
2139+
msg = ("The 'convert' parameter is deprecated "
2140+
"and will be removed in a future version.")
2141+
warnings.warn(msg, FutureWarning, stacklevel=2)
2142+
21372143
new_data = self._data.take(indices,
21382144
axis=self._get_block_manager_axis(axis),
2139-
convert=True, verify=True)
2145+
verify=True)
21402146
result = self._constructor(new_data).__finalize__(self)
21412147

21422148
# maybe set copy if we didn't actually change the index
@@ -2245,9 +2251,9 @@ def xs(self, key, axis=0, level=None, drop_level=True):
22452251
if isinstance(loc, np.ndarray):
22462252
if loc.dtype == np.bool_:
22472253
inds, = loc.nonzero()
2248-
return self.take(inds, axis=axis, convert=False)
2254+
return self.take(inds, axis=axis)
22492255
else:
2250-
return self.take(loc, axis=axis, convert=True)
2256+
return self.take(loc, axis=axis)
22512257

22522258
if not is_scalar(loc):
22532259
new_index = self.index[loc]
@@ -5066,7 +5072,7 @@ def at_time(self, time, asof=False):
50665072
"""
50675073
try:
50685074
indexer = self.index.indexer_at_time(time, asof=asof)
5069-
return self.take(indexer, convert=False)
5075+
return self.take(indexer)
50705076
except AttributeError:
50715077
raise TypeError('Index must be DatetimeIndex')
50725078

@@ -5090,7 +5096,7 @@ def between_time(self, start_time, end_time, include_start=True,
50905096
indexer = self.index.indexer_between_time(
50915097
start_time, end_time, include_start=include_start,
50925098
include_end=include_end)
5093-
return self.take(indexer, convert=False)
5099+
return self.take(indexer)
50945100
except AttributeError:
50955101
raise TypeError('Index must be DatetimeIndex')
50965102

pandas/core/groupby.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def _set_grouper(self, obj, sort=False):
317317
indexer = self.indexer = ax.argsort(kind='mergesort')
318318
ax = ax.take(indexer)
319319
obj = obj.take(indexer, axis=self.axis,
320-
convert=False, is_copy=False)
320+
is_copy=False)
321321

322322
self.obj = obj
323323
self.grouper = ax
@@ -640,7 +640,7 @@ def get_group(self, name, obj=None):
640640
if not len(inds):
641641
raise KeyError(name)
642642

643-
return obj.take(inds, axis=self.axis, convert=False)
643+
return obj.take(inds, axis=self.axis)
644644

645645
def __iter__(self):
646646
"""
@@ -2190,7 +2190,7 @@ def _aggregate_series_fast(self, obj, func):
21902190
# avoids object / Series creation overhead
21912191
dummy = obj._get_values(slice(None, 0)).to_dense()
21922192
indexer = get_group_index_sorter(group_index, ngroups)
2193-
obj = obj.take(indexer, convert=False).to_dense()
2193+
obj = obj.take(indexer).to_dense()
21942194
group_index = algorithms.take_nd(
21952195
group_index, indexer, allow_fill=False)
21962196
grouper = lib.SeriesGrouper(obj, func, group_index, ngroups,
@@ -4419,7 +4419,7 @@ def __iter__(self):
44194419
yield i, self._chop(sdata, slice(start, end))
44204420

44214421
def _get_sorted_data(self):
4422-
return self.data.take(self.sort_idx, axis=self.axis, convert=False)
4422+
return self.data.take(self.sort_idx, axis=self.axis)
44234423

44244424
def _chop(self, sdata, slice_obj):
44254425
return sdata.iloc[slice_obj]

pandas/core/indexing.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ def _getitem_iterable(self, key, axis=0):
10921092
if is_bool_indexer(key):
10931093
key = check_bool_indexer(labels, key)
10941094
inds, = key.nonzero()
1095-
return self.obj.take(inds, axis=axis, convert=False)
1095+
return self.obj.take(inds, axis=axis)
10961096
else:
10971097
# Have the index compute an indexer or return None
10981098
# if it cannot handle; we only act on all found values
@@ -1125,15 +1125,14 @@ def _getitem_iterable(self, key, axis=0):
11251125
keyarr)
11261126

11271127
if new_indexer is not None:
1128-
result = self.obj.take(indexer[indexer != -1], axis=axis,
1129-
convert=False)
1128+
result = self.obj.take(indexer[indexer != -1], axis=axis)
11301129

11311130
result = result._reindex_with_indexers(
11321131
{axis: [new_target, new_indexer]},
11331132
copy=True, allow_dups=True)
11341133

11351134
else:
1136-
result = self.obj.take(indexer, axis=axis, convert=False)
1135+
result = self.obj.take(indexer, axis=axis)
11371136

11381137
return result
11391138

@@ -1263,7 +1262,7 @@ def _get_slice_axis(self, slice_obj, axis=0):
12631262
if isinstance(indexer, slice):
12641263
return self._slice(indexer, axis=axis, kind='iloc')
12651264
else:
1266-
return self.obj.take(indexer, axis=axis, convert=False)
1265+
return self.obj.take(indexer, axis=axis)
12671266

12681267

12691268
class _IXIndexer(_NDFrameIndexer):
@@ -1348,7 +1347,7 @@ def _getbool_axis(self, key, axis=0):
13481347
key = check_bool_indexer(labels, key)
13491348
inds, = key.nonzero()
13501349
try:
1351-
return self.obj.take(inds, axis=axis, convert=False)
1350+
return self.obj.take(inds, axis=axis)
13521351
except Exception as detail:
13531352
raise self._exception(detail)
13541353

@@ -1365,7 +1364,7 @@ def _get_slice_axis(self, slice_obj, axis=0):
13651364
if isinstance(indexer, slice):
13661365
return self._slice(indexer, axis=axis, kind='iloc')
13671366
else:
1368-
return self.obj.take(indexer, axis=axis, convert=False)
1367+
return self.obj.take(indexer, axis=axis)
13691368

13701369

13711370
class _LocIndexer(_LocationIndexer):
@@ -1703,7 +1702,7 @@ def _get_slice_axis(self, slice_obj, axis=0):
17031702
if isinstance(slice_obj, slice):
17041703
return self._slice(slice_obj, axis=axis, kind='iloc')
17051704
else:
1706-
return self.obj.take(slice_obj, axis=axis, convert=False)
1705+
return self.obj.take(slice_obj, axis=axis)
17071706

17081707
def _get_list_axis(self, key, axis=0):
17091708
"""
@@ -1719,7 +1718,7 @@ def _get_list_axis(self, key, axis=0):
17191718
Series object
17201719
"""
17211720
try:
1722-
return self.obj.take(key, axis=axis, convert=False)
1721+
return self.obj.take(key, axis=axis)
17231722
except IndexError:
17241723
# re-raise with different error message
17251724
raise IndexError("positional indexers are out-of-bounds")

pandas/core/series.py

+24-7
Original file line numberDiff line numberDiff line change
@@ -2561,27 +2561,44 @@ def memory_usage(self, index=True, deep=False):
25612561

25622562
def take(self, indices, axis=0, convert=True, is_copy=False, **kwargs):
25632563
"""
2564-
return Series corresponding to requested indices
2564+
Return a Series corresponding to requested indices.
25652565
25662566
Parameters
25672567
----------
2568-
indices : list / array of ints
2569-
convert : translate negative to positive indices (default)
2568+
indices : array-like
2569+
An array of ints indicating which positions to take.
2570+
axis : int, default 0
2571+
The axis on which to select elements. This implementation
2572+
ignores this parameter because there is only one axis.
2573+
convert : bool, default True
2574+
.. deprecated:: 0.21.0
2575+
2576+
Whether to translate negative to positive indices. In the future,
2577+
this function will always translate negative to positive indices.
2578+
2579+
is_copy : bool, default False
2580+
Whether to return a copy of the original object or not. This
2581+
implementation ignores this parameter.
25702582
25712583
Returns
25722584
-------
25732585
taken : Series
2586+
An array-like containing the elements taken from the object.
25742587
2575-
See also
2588+
See Also
25762589
--------
25772590
numpy.ndarray.take
2591+
numpy.take
25782592
"""
2593+
25792594
if kwargs:
25802595
nv.validate_take(tuple(), kwargs)
25812596

2582-
# check/convert indicies here
2583-
if convert:
2584-
indices = maybe_convert_indices(indices, len(self._get_axis(axis)))
2597+
# Check / convert indices here.
2598+
if not convert:
2599+
msg = ("The 'convert' parameter is deprecated "
2600+
"and will be removed in a future version.")
2601+
warnings.warn(msg, FutureWarning, stacklevel=2)
25852602

25862603
indices = _ensure_platform_int(indices)
25872604
new_index = self.index.take(indices)

pandas/core/sparse/series.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -604,14 +604,38 @@ def sparse_reindex(self, new_index):
604604

605605
def take(self, indices, axis=0, convert=True, *args, **kwargs):
606606
"""
607-
Sparse-compatible version of ndarray.take
607+
Sparse-compatible version of ndarray.take.
608+
609+
Parameters
610+
----------
611+
indices : array-like
612+
An array of ints indicating which positions to take.
613+
axis : int, default 0
614+
The axis on which to select elements. This implementation
615+
ignores this parameter because there is only one axis.
616+
convert : bool, default True
617+
.. deprecated:: 0.21.0
618+
619+
This parameter is not respected by the function.
608620
609621
Returns
610622
-------
611-
taken : ndarray
623+
taken : numpy.ndarray
624+
An array-like containing the elements taken from the object.
625+
626+
See Also
627+
--------
628+
numpy.ndarray.take
629+
numpy.take
612630
"""
613631

614632
convert = nv.validate_take_with_convert(convert, args, kwargs)
633+
634+
if not convert:
635+
msg = ("The 'convert' parameter is deprecated "
636+
"and will be removed in a future version.")
637+
warnings.warn(msg, FutureWarning, stacklevel=2)
638+
615639
new_values = SparseArray.take(self.values, indices)
616640
new_index = self.index.take(indices)
617641
return self._constructor(new_values,

pandas/tests/frame/test_axis_select_reindex.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -787,14 +787,18 @@ def test_take(self):
787787
expected = df.loc[:, ['D', 'B', 'C', 'A']]
788788
assert_frame_equal(result, expected, check_names=False)
789789

790-
# neg indicies
790+
# negative indices
791791
order = [2, 1, -1]
792792
for df in [self.frame]:
793793

794794
result = df.take(order, axis=0)
795795
expected = df.reindex(df.index.take(order))
796796
assert_frame_equal(result, expected)
797797

798+
with tm.assert_produces_warning(FutureWarning):
799+
result = df.take(order, convert=False, axis=0)
800+
assert_frame_equal(result, expected)
801+
798802
# axis = 1
799803
result = df.take(order, axis=1)
800804
expected = df.loc[:, ['C', 'B', 'D']]
@@ -819,7 +823,7 @@ def test_take(self):
819823
expected = df.loc[:, ['foo', 'B', 'C', 'A', 'D']]
820824
assert_frame_equal(result, expected)
821825

822-
# neg indicies
826+
# negative indices
823827
order = [4, 1, -2]
824828
for df in [self.mixed_frame]:
825829

pandas/tests/indexing/test_loc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,11 @@ def gen_test(l, l2):
581581

582582
def gen_expected(df, mask):
583583
l = len(mask)
584-
return pd.concat([df.take([0], convert=False),
584+
return pd.concat([df.take([0]),
585585
DataFrame(np.ones((l, len(columns))),
586586
index=[0] * l,
587587
columns=columns),
588-
df.take(mask[1:], convert=False)])
588+
df.take(mask[1:])])
589589

590590
df = gen_test(900, 100)
591591
assert not df.index.is_unique

pandas/tests/series/test_indexing.py

+17
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,23 @@ def test_setitem_with_tz_dst(self):
10661066
s.iloc[[1, 2]] = vals
10671067
tm.assert_series_equal(s, exp)
10681068

1069+
def test_take(self):
1070+
s = Series([-1, 5, 6, 2, 4])
1071+
1072+
actual = s.take([1, 3, 4])
1073+
expected = Series([5, 2, 4], index=[1, 3, 4])
1074+
tm.assert_series_equal(actual, expected)
1075+
1076+
actual = s.take([-1, 3, 4])
1077+
expected = Series([4, 2, 4], index=[4, 3, 4])
1078+
tm.assert_series_equal(actual, expected)
1079+
1080+
pytest.raises(IndexError, s.take, [1, 10])
1081+
pytest.raises(IndexError, s.take, [2, 5])
1082+
1083+
with tm.assert_produces_warning(FutureWarning):
1084+
s.take([-1, 3, 4], convert=False)
1085+
10691086
def test_where(self):
10701087
s = Series(np.random.randn(5))
10711088
cond = s > 0

0 commit comments

Comments
 (0)