Skip to content
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

DEPR: is_copy arg of take #30615

Merged
merged 13 commits into from
Jan 6, 2020
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ Deprecations
- The deprecated internal attributes ``_start``, ``_stop`` and ``_step`` of :class:`RangeIndex` now raise a ``FutureWarning`` instead of a ``DeprecationWarning`` (:issue:`26581`)
- The ``pandas.util.testing`` module has been deprecated. Use the public API in ``pandas.testing`` documented at :ref:`api.general.testing` (:issue:`16232`).
- ``pandas.SparseArray`` has been deprecated. Use ``pandas.arrays.SparseArray`` (:class:`arrays.SparseArray`) instead. (:issue:`30642`)
- The parameter ``is_copy`` of :meth:`DataFrame.take` has been deprecated and will be removed in a future version. (:issue:`27357`)

**Selecting Columns from a Grouped DataFrame**

Expand Down
19 changes: 16 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3275,7 +3275,7 @@ def _clear_item_cache(self) -> None:
# Indexing Methods

def take(
self: FrameOrSeries, indices, axis=0, is_copy: bool_t = True, **kwargs
self: FrameOrSeries, indices, axis=0, is_copy: Optional[bool_t] = None, **kwargs
) -> FrameOrSeries:
"""
Return the elements in the given *positional* indices along an axis.
Expand All @@ -3293,6 +3293,8 @@ def take(
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:: 1.0.0
**kwargs
For compatibility with :meth:`numpy.take`. Has no effect on the
output.
Expand Down Expand Up @@ -3351,6 +3353,16 @@ 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. "
"take will always return a copy in the future.",
FutureWarning,
stacklevel=2,
)
else:
is_copy = True

nv.validate_take(tuple(), kwargs)

self._consolidate_inplace()
Expand Down Expand Up @@ -5014,7 +5026,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)
ryankarlos marked this conversation as resolved.
Show resolved Hide resolved

_shared_docs[
"pipe"
Expand Down Expand Up @@ -7011,7 +7023,8 @@ def asof(self, where, subset=None):

# mask the missing
missing = locs == -1
data = self.take(locs, is_copy=False)
d = self.take(locs)
data = d.copy()
Copy link
Member

Choose a reason for hiding this comment

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

Were there test failures if you didn't do this d.copy() step?

Copy link
Contributor Author

@ryankarlos ryankarlos Jan 7, 2020

Choose a reason for hiding this comment

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

Yes, it was breaking builds with SettingwithCopyError see #30615 (comment) and lower bit of #30615 (comment)

data.index = where
data.loc[missing] = np.nan
return data if is_list else data.iloc[-1]
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def _set_grouper(self, obj: FrameOrSeries, sort: bool = False):
# use stable sort to support first, last, nth
indexer = self.indexer = ax.argsort(kind="mergesort")
ax = ax.take(indexer)
obj = obj.take(indexer, axis=self.axis, is_copy=False)
obj = obj.take(indexer, axis=self.axis)

self.obj = obj
self.grouper = ax
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/frame/methods/test_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def test_basic(self, date_range_frame):
ub = df.index[30]

dates = list(dates)

result = df.asof(dates)
assert result.notna().all(1).all()

Expand Down Expand Up @@ -65,6 +66,7 @@ def test_missing(self, date_range_frame):
# no match found - `where` value before earliest date in index
N = 10
df = date_range_frame.iloc[:N].copy()

result = df.asof("1989-12-31")

expected = Series(
Expand Down Expand Up @@ -132,5 +134,6 @@ def test_time_zone_aware_index(self, stamp, expected):
Timestamp("2018-01-01 22:35:10.550+00:00"),
],
)

result = df.asof(stamp)
tm.assert_series_equal(result, expected)
12 changes: 12 additions & 0 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,18 @@ def test_take_invalid_kwargs(self):
with pytest.raises(ValueError, match=msg):
obj.take(indices, mode="clip")

def test_depr_take_kwarg_is_copy(self):
# GH 27357
df = DataFrame({"A": [1, 2, 3]})
msg = (
"is_copy is deprecated and will be removed in a future version. "
"take will always return a copy in the future."
)
with tm.assert_produces_warning(FutureWarning) as w:
df.take([0, 1], is_copy=True)

assert w[0].message.args[0] == msg

def test_equals(self):
s1 = pd.Series([1, 2, 3], index=[0, 2, 1])
s2 = s1.copy()
Expand Down