Skip to content

DEPR: is_copy arg of take closes #27357 #28751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 1.0.0 at this point

**kwargs
For compatibility with :meth:`numpy.take`. Has no effect on the
output.
Expand Down Expand Up @@ -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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add that 'take' will always return a copy in the future?

FutureWarning,
stacklevel=2,
)
is_copy = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this assignment even required? Not used later in the function body right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a if is_copy: a few lines below

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But, we should only put it to True if it was None (to keep the existing behaviour), so put it in an else block ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used:

        # Maybe set copy if we didn't actually change the index.
        if is_copy:
            if not result._get_axis(axis).equals(self._get_axis(axis)):
                result._set_is_copy(self)
        return result

But is_copy is already deprecated. I think it needs to be removed for 1.0. Since this is going into 1.0 I am thinking now that instead of deprecating this parameter it needs to be removed all together, along with is_copy ?


nv.validate_take(tuple(), kwargs)

self._consolidate_inplace()
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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]
Expand Down
7 changes: 6 additions & 1 deletion pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference issue number above

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()
Expand Down