Skip to content

DEPR: ‘names’ param in Index.copy #44916

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 6 commits into from
Dec 16, 2021
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ Other Deprecations
- Deprecated passing arguments as positional for :func:`read_fwf` other than ``filepath_or_buffer`` (:issue:`41485`):
- Deprecated passing ``skipna=None`` for :meth:`DataFrame.mad` and :meth:`Series.mad`, pass ``skipna=True`` instead (:issue:`44580`)
- Deprecated :meth:`DateOffset.apply`, use ``offset + other`` instead (:issue:`44522`)
- Deprecated parameter ``names`` in :meth:`Index.copy` (:issue:`44916`)
- A deprecation warning is now shown for :meth:`DataFrame.to_latex` indicating the arguments signature may change and emulate more the arguments to :meth:`.Styler.to_latex` in future versions (:issue:`44411`)
-

Expand Down
11 changes: 11 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,9 @@ def copy(
names : list-like, optional
Kept for compatibility with MultiIndex. Should not be used.

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

Returns
-------
Index
Expand All @@ -1188,6 +1191,14 @@ 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]
if deep:
new_data = self._data.copy()
Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,13 +1260,19 @@ def test_copy_name2(self):
assert index.name == "MyName"
assert index2.name == "NewName"

index3 = index.copy(names=["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