From 49a2fff448c531c0905491d4e7e0b4c2f0cd455c Mon Sep 17 00:00:00 2001 From: Chris St Pierre Date: Mon, 30 Sep 2019 15:53:59 +0300 Subject: [PATCH] DEPR: is_copy arg of take closes #27357 --- pandas/core/generic.py | 16 +++++++++++++--- pandas/tests/generic/test_generic.py | 7 ++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a3b9bec494854..541ef54eafcf4 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3355,7 +3355,7 @@ def _clear_item_cache(self): # ---------------------------------------------------------------------- # Indexing Methods - def take(self, indices, axis=0, is_copy=True, **kwargs): + def take(self, indices, axis=0, is_copy=None, **kwargs): """ Return the elements in the given *positional* indices along an axis. @@ -3372,6 +3372,8 @@ def take(self, indices, axis=0, is_copy=True, **kwargs): selecting rows, ``1`` means that we are selecting columns. is_copy : bool, default True Whether to return a copy of the original object or not. + + .. deprecated:: 0.25.2 **kwargs For compatibility with :meth:`numpy.take`. Has no effect on the output. @@ -3430,6 +3432,14 @@ class max_speed 1 monkey mammal NaN 3 lion mammal 80.5 """ + if is_copy is not None: + warnings.warn( + "is_copy is deprecated and will be removed in a future version", + FutureWarning, + stacklevel=2, + ) + is_copy = True + nv.validate_take(tuple(), kwargs) self._consolidate_inplace() @@ -5014,7 +5024,7 @@ def sample( ) locs = rs.choice(axis_length, size=n, replace=replace, p=weights) - return self.take(locs, axis=axis, is_copy=False) + return self.take(locs, axis=axis) _shared_docs[ "pipe" @@ -7255,7 +7265,7 @@ def asof(self, where, subset=None): # mask the missing missing = locs == -1 - data = self.take(locs, is_copy=False) + data = self.take(locs) data.index = where data.loc[missing] = np.nan return data if is_list else data.iloc[-1] diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index 7b9e50ebbf342..98c4c48e0292d 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -589,7 +589,7 @@ def test_pct_change(self, periods, fill_method, limit, exp): class TestNDFrame: # tests that don't fit elsewhere - def test_sample(sel): + def test_sample(self): # Fixes issue: 2419 # additional specific object based tests @@ -800,6 +800,11 @@ def test_take_invalid_kwargs(self): with pytest.raises(ValueError, match=msg): obj.take(indices, mode="clip") + def test_take_deprecated_kwarg_is_copy(self): + df = DataFrame([1, 2]) + with tm.assert_produces_warning(FutureWarning): + df.take([0, 1], is_copy=True) + def test_equals(self): s1 = pd.Series([1, 2, 3], index=[0, 2, 1]) s2 = s1.copy()