Skip to content

Commit 7796be6

Browse files
ryankarlosjreback
authored andcommitted
DEPR: is_copy arg of take (#30615)
1 parent a94ff3b commit 7796be6

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ Deprecations
579579
- The deprecated internal attributes ``_start``, ``_stop`` and ``_step`` of :class:`RangeIndex` now raise a ``FutureWarning`` instead of a ``DeprecationWarning`` (:issue:`26581`)
580580
- The ``pandas.util.testing`` module has been deprecated. Use the public API in ``pandas.testing`` documented at :ref:`api.general.testing` (:issue:`16232`).
581581
- ``pandas.SparseArray`` has been deprecated. Use ``pandas.arrays.SparseArray`` (:class:`arrays.SparseArray`) instead. (:issue:`30642`)
582+
- The parameter ``is_copy`` of :meth:`DataFrame.take` has been deprecated and will be removed in a future version. (:issue:`27357`)
582583

583584
**Selecting Columns from a Grouped DataFrame**
584585

pandas/core/generic.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -3275,7 +3275,7 @@ def _clear_item_cache(self) -> None:
32753275
# Indexing Methods
32763276

32773277
def take(
3278-
self: FrameOrSeries, indices, axis=0, is_copy: bool_t = True, **kwargs
3278+
self: FrameOrSeries, indices, axis=0, is_copy: Optional[bool_t] = None, **kwargs
32793279
) -> FrameOrSeries:
32803280
"""
32813281
Return the elements in the given *positional* indices along an axis.
@@ -3293,6 +3293,8 @@ def take(
32933293
selecting rows, ``1`` means that we are selecting columns.
32943294
is_copy : bool, default True
32953295
Whether to return a copy of the original object or not.
3296+
3297+
.. deprecated:: 1.0.0
32963298
**kwargs
32973299
For compatibility with :meth:`numpy.take`. Has no effect on the
32983300
output.
@@ -3351,6 +3353,16 @@ class max_speed
33513353
1 monkey mammal NaN
33523354
3 lion mammal 80.5
33533355
"""
3356+
if is_copy is not None:
3357+
warnings.warn(
3358+
"is_copy is deprecated and will be removed in a future version. "
3359+
"take will always return a copy in the future.",
3360+
FutureWarning,
3361+
stacklevel=2,
3362+
)
3363+
else:
3364+
is_copy = True
3365+
33543366
nv.validate_take(tuple(), kwargs)
33553367

33563368
self._consolidate_inplace()
@@ -5014,7 +5026,7 @@ def sample(
50145026
)
50155027

50165028
locs = rs.choice(axis_length, size=n, replace=replace, p=weights)
5017-
return self.take(locs, axis=axis, is_copy=False)
5029+
return self.take(locs, axis=axis)
50185030

50195031
_shared_docs[
50205032
"pipe"
@@ -7011,7 +7023,8 @@ def asof(self, where, subset=None):
70117023

70127024
# mask the missing
70137025
missing = locs == -1
7014-
data = self.take(locs, is_copy=False)
7026+
d = self.take(locs)
7027+
data = d.copy()
70157028
data.index = where
70167029
data.loc[missing] = np.nan
70177030
return data if is_list else data.iloc[-1]

pandas/core/groupby/grouper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def _set_grouper(self, obj: FrameOrSeries, sort: bool = False):
194194
# use stable sort to support first, last, nth
195195
indexer = self.indexer = ax.argsort(kind="mergesort")
196196
ax = ax.take(indexer)
197-
obj = obj.take(indexer, axis=self.axis, is_copy=False)
197+
obj = obj.take(indexer, axis=self.axis)
198198

199199
self.obj = obj
200200
self.grouper = ax

pandas/tests/frame/methods/test_asof.py

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def test_basic(self, date_range_frame):
3030
ub = df.index[30]
3131

3232
dates = list(dates)
33+
3334
result = df.asof(dates)
3435
assert result.notna().all(1).all()
3536

@@ -65,6 +66,7 @@ def test_missing(self, date_range_frame):
6566
# no match found - `where` value before earliest date in index
6667
N = 10
6768
df = date_range_frame.iloc[:N].copy()
69+
6870
result = df.asof("1989-12-31")
6971

7072
expected = Series(
@@ -132,5 +134,6 @@ def test_time_zone_aware_index(self, stamp, expected):
132134
Timestamp("2018-01-01 22:35:10.550+00:00"),
133135
],
134136
)
137+
135138
result = df.asof(stamp)
136139
tm.assert_series_equal(result, expected)

pandas/tests/generic/test_generic.py

+12
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,18 @@ def test_take_invalid_kwargs(self):
820820
with pytest.raises(ValueError, match=msg):
821821
obj.take(indices, mode="clip")
822822

823+
def test_depr_take_kwarg_is_copy(self):
824+
# GH 27357
825+
df = DataFrame({"A": [1, 2, 3]})
826+
msg = (
827+
"is_copy is deprecated and will be removed in a future version. "
828+
"take will always return a copy in the future."
829+
)
830+
with tm.assert_produces_warning(FutureWarning) as w:
831+
df.take([0, 1], is_copy=True)
832+
833+
assert w[0].message.args[0] == msg
834+
823835
def test_equals(self):
824836
s1 = pd.Series([1, 2, 3], index=[0, 2, 1])
825837
s2 = s1.copy()

0 commit comments

Comments
 (0)