Skip to content

Commit

Permalink
DEPR: ‘names’ param in Index.copy (#44916)
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 authored Dec 16, 2021
1 parent ccc1a25 commit adfc78b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
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`)
- Deprecated :meth:`Categorical.replace`, use :meth:`Series.replace` instead (:issue:`44929`)
-
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

0 comments on commit adfc78b

Please sign in to comment.