Skip to content

Commit d2539ef

Browse files
Revert "ENH: set_index copy kwd (pandas-dev#48043)"
This reverts commit 9716fcb.
1 parent 904f66e commit d2539ef

File tree

3 files changed

+2
-36
lines changed

3 files changed

+2
-36
lines changed

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ Other enhancements
329329
- :meth:`DataFrame.quantile` gained a ``method`` argument that can accept ``table`` to evaluate multi-column quantiles (:issue:`43881`)
330330
- :class:`Interval` now supports checking whether one interval is contained by another interval (:issue:`46613`)
331331
- Added ``copy`` keyword to :meth:`Series.set_axis` and :meth:`DataFrame.set_axis` to allow user to set axis on a new object without necessarily copying the underlying data (:issue:`47932`)
332-
- :meth:`DataFrame.set_index` now supports a ``copy`` keyword. If ``False``, the underlying data is not copied when a new :class:`DataFrame` is returned (:issue:`48043`)
333332
- The method :meth:`.ExtensionArray.factorize` accepts ``use_na_sentinel=False`` for determining how null values are to be treated (:issue:`46601`)
333+
- :meth:`Series.add_suffix`, :meth:`DataFrame.add_suffix`, :meth:`Series.add_prefix` and :meth:`DataFrame.add_prefix` support a ``copy`` argument. If ``False``, the underlying data is not copied in the returned object (:issue:`47934`)
334334

335335
.. ---------------------------------------------------------------------------
336336
.. _whatsnew_150.notable_bug_fixes:

pandas/core/frame.py

+1-16
Original file line numberDiff line numberDiff line change
@@ -5858,7 +5858,6 @@ def set_index(
58585858
append: bool = ...,
58595859
inplace: Literal[False] = ...,
58605860
verify_integrity: bool = ...,
5861-
copy: bool | lib.NoDefault = ...,
58625861
) -> DataFrame:
58635862
...
58645863

@@ -5871,7 +5870,6 @@ def set_index(
58715870
append: bool = ...,
58725871
inplace: Literal[True],
58735872
verify_integrity: bool = ...,
5874-
copy: bool | lib.NoDefault = ...,
58755873
) -> None:
58765874
...
58775875

@@ -5883,7 +5881,6 @@ def set_index(
58835881
append: bool = False,
58845882
inplace: bool = False,
58855883
verify_integrity: bool = False,
5886-
copy: bool | lib.NoDefault = lib.no_default,
58875884
) -> DataFrame | None:
58885885
"""
58895886
Set the DataFrame index using existing columns.
@@ -5910,11 +5907,6 @@ def set_index(
59105907
Check the new index for duplicates. Otherwise defer the check until
59115908
necessary. Setting to False will improve the performance of this
59125909
method.
5913-
copy : bool, default True
5914-
Whether to make a copy of the underlying data when returning a new
5915-
DataFrame.
5916-
5917-
.. versionadded:: 1.5.0
59185910
59195911
Returns
59205912
-------
@@ -5980,13 +5972,6 @@ def set_index(
59805972
4 16 10 2014 31
59815973
"""
59825974
inplace = validate_bool_kwarg(inplace, "inplace")
5983-
if inplace:
5984-
if copy is not lib.no_default:
5985-
raise ValueError("Cannot specify copy when inplace=True")
5986-
copy = False
5987-
elif copy is lib.no_default:
5988-
copy = True
5989-
59905975
self._check_inplace_and_allows_duplicate_labels(inplace)
59915976
if not isinstance(keys, list):
59925977
keys = [keys]
@@ -6022,7 +6007,7 @@ def set_index(
60226007
if inplace:
60236008
frame = self
60246009
else:
6025-
frame = self.copy(deep=copy)
6010+
frame = self.copy()
60266011

60276012
arrays = []
60286013
names: list[Hashable] = []

pandas/tests/frame/methods/test_set_index.py

-19
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,6 @@
2525

2626

2727
class TestSetIndex:
28-
def test_set_index_copy(self):
29-
# GH#48043
30-
df = DataFrame({"A": [1, 2], "B": [3, 4], "C": [5, 6]})
31-
expected = DataFrame({"B": [3, 4], "C": [5, 6]}, index=Index([1, 2], name="A"))
32-
33-
res = df.set_index("A", copy=True)
34-
tm.assert_frame_equal(res, expected)
35-
assert not any(tm.shares_memory(df[col], res[col]) for col in res.columns)
36-
37-
res = df.set_index("A", copy=False)
38-
tm.assert_frame_equal(res, expected)
39-
assert all(tm.shares_memory(df[col], res[col]) for col in res.columns)
40-
41-
msg = "Cannot specify copy when inplace=True"
42-
with pytest.raises(ValueError, match=msg):
43-
df.set_index("A", inplace=True, copy=True)
44-
with pytest.raises(ValueError, match=msg):
45-
df.set_index("A", inplace=True, copy=False)
46-
4728
def test_set_index_multiindex(self):
4829
# segfault in GH#3308
4930
d = {"t1": [2, 2.5, 3], "t2": [4, 5, 6]}

0 commit comments

Comments
 (0)