Skip to content

DEP: Enforce deprecation of names and dtype in index copy and codes and levels in midx.copy #49087

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

Merged
merged 4 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 2 additions & 31 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
Axes,
Axis,
DropKeep,
Dtype,
DtypeObj,
F,
IgnoreRaise,
Expand Down Expand Up @@ -1292,29 +1291,17 @@ def copy(
self: _IndexT,
name: Hashable | None = None,
deep: bool = False,
dtype: Dtype | None = None,
names: Sequence[Hashable] | None = None,
) -> _IndexT:
"""
Make a copy of this object.

Name and dtype sets those attributes on the new object.
Name is set on the new object.

Parameters
----------
name : Label, optional
Set name for new object.
deep : bool, default False
dtype : numpy dtype or pandas type, optional
Set dtype for new object.

.. deprecated:: 1.2.0
use ``astype`` method instead.
names : list-like, optional
Kept for compatibility with MultiIndex. Should not be used.

.. deprecated:: 1.4.0
use ``name`` instead.

Returns
-------
Expand All @@ -1326,29 +1313,13 @@ def copy(
In most cases, there should be no functional difference from using
``deep``, but if ``deep`` is passed it will attempt to deepcopy.
"""
if names is not None:
warnings.warn(
"parameter names is deprecated and will be removed in a future "
"version. Use the name parameter instead.",
FutureWarning,
stacklevel=find_stack_level(),
)

name = self._validate_names(name=name, names=names, deep=deep)[0]
name = self._validate_names(name=name, deep=deep)[0]
if deep:
new_data = self._data.copy()
new_index = type(self)._simple_new(new_data, name=name)
else:
new_index = self._rename(name=name)

if dtype:
warnings.warn(
"parameter dtype is deprecated and will be removed in a future "
"version. Use the astype method instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
new_index = new_index.astype(dtype)
return new_index

@final
Expand Down
44 changes: 3 additions & 41 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,9 +1174,6 @@ def _view(self) -> MultiIndex:
def copy( # type: ignore[override]
self,
names=None,
dtype=None,
levels=None,
codes=None,
deep: bool = False,
name=None,
):
Expand All @@ -1187,15 +1184,6 @@ def copy( # type: ignore[override]
Parameters
----------
names : sequence, optional
dtype : numpy dtype or pandas type, optional

.. deprecated:: 1.2.0
levels : sequence, optional

.. deprecated:: 1.2.0
codes : sequence, optional

.. deprecated:: 1.2.0
deep : bool, default False
name : Label
Kept for compatibility with 1-dimensional Index. Should not be used.
Expand All @@ -1212,30 +1200,13 @@ def copy( # type: ignore[override]
"""
names = self._validate_names(name=name, names=names, deep=deep)
keep_id = not deep
if levels is not None:
warnings.warn(
"parameter levels is deprecated and will be removed in a future "
"version. Use the set_levels method instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
keep_id = False
if codes is not None:
warnings.warn(
"parameter codes is deprecated and will be removed in a future "
"version. Use the set_codes method instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
keep_id = False
levels, codes = None, None

if deep:
from copy import deepcopy

if levels is None:
levels = deepcopy(self.levels)
if codes is None:
codes = deepcopy(self.codes)
levels = deepcopy(self.levels)
codes = deepcopy(self.codes)

levels = levels if levels is not None else self.levels
codes = codes if codes is not None else self.codes
Expand All @@ -1251,15 +1222,6 @@ def copy( # type: ignore[override]
new_index._cache.pop("levels", None) # GH32669
if keep_id:
new_index._id = self._id

if dtype:
warnings.warn(
"parameter dtype is deprecated and will be removed in a future "
"version. Use the astype method instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
new_index = new_index.astype(dtype)
return new_index

def __array__(self, dtype=None) -> np.ndarray:
Expand Down
19 changes: 2 additions & 17 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,24 +456,9 @@ def _view(self: RangeIndex) -> RangeIndex:
return result

@doc(Int64Index.copy)
def copy(
self,
name: Hashable = None,
deep: bool = False,
dtype: Dtype | None = None,
names=None,
):
name = self._validate_names(name=name, names=names, deep=deep)[0]
def copy(self, name: Hashable = None, deep: bool = False):
name = self._validate_names(name=name, deep=deep)[0]
new_index = self._rename(name=name)

if dtype:
warnings.warn(
"parameter dtype is deprecated and will be removed in a future "
"version. Use the astype method instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
new_index = new_index.astype(dtype)
return new_index

def _minmax(self, meth: str):
Expand Down
21 changes: 0 additions & 21 deletions pandas/tests/indexes/multi/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,6 @@ def test_copy_method_kwargs(deep, kwarg, value):
assert getattr(idx_copy, kwarg) == value


@pytest.mark.parametrize("deep", [True, False])
@pytest.mark.parametrize(
"param_name, param_value",
[
("levels", [["foo2", "bar2"], ["fizz2", "buzz2"]]),
("codes", [[1, 0, 0, 0], [1, 1, 0, 0]]),
],
)
def test_copy_deprecated_parameters(deep, param_name, param_value):
# gh-36685
idx = MultiIndex(
levels=[["foo", "bar"], ["fizz", "buzz"]],
codes=[[0, 0, 0, 1], [0, 0, 1, 1]],
names=["first", "second"],
)
with tm.assert_produces_warning(FutureWarning):
idx_copy = idx.copy(deep=deep, **{param_name: param_value})

assert [list(i) for i in getattr(idx_copy, param_name)] == param_value


def test_copy_deep_false_retains_id():
# GH#47878
idx = MultiIndex(
Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/indexes/test_any_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ def test_hash_error(index):
hash(index)


def test_copy_dtype_deprecated(index):
# GH#35853
with tm.assert_produces_warning(FutureWarning):
index.copy(dtype=object)


def test_mutability(index):
if not len(index):
return
Expand Down
13 changes: 0 additions & 13 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1305,19 +1305,6 @@ def test_copy_name2(self):
assert index.name == "MyName"
assert index2.name == "NewName"

with tm.assert_produces_warning(FutureWarning):
index3 = index.copy(names=["NewName"])
tm.assert_index_equal(index, index3, check_names=False)
assert index.name == "MyName"
assert index.names == ["MyName"]
assert index3.name == "NewName"
assert index3.names == ["NewName"]

def test_copy_names_deprecated(self, simple_index):
# GH44916
with tm.assert_produces_warning(FutureWarning):
simple_index.copy(names=["a"])

def test_unique_na(self):
idx = Index([2, np.nan, 2, 1], name="my_index")
expected = Index([2, np.nan, 1], name="my_index")
Expand Down