Skip to content

Commit f6e9ac1

Browse files
committed
ENH: Deprecate non-keyword arguments for Index.set_names.
1 parent 896256e commit f6e9ac1

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ Deprecations
648648
- Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`)
649649
- Deprecated behavior of :meth:`DatetimeIndex.union` with mixed timezones; in a future version both will be cast to UTC instead of object dtype (:issue:`39328`)
650650
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
651+
- Deprecated passing arguments as positional in :meth:`Index.set_names` (except for ``names``) (:issue:`41485`)
651652

652653
.. ---------------------------------------------------------------------------
653654

pandas/core/indexes/base.py

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
from pandas.util._decorators import (
5555
Appender,
5656
cache_readonly,
57+
deprecate_nonkeyword_arguments,
5758
doc,
5859
)
5960

@@ -1526,6 +1527,7 @@ def _set_names(self, values, level=None) -> None:
15261527

15271528
names = property(fset=_set_names, fget=_get_names)
15281529

1530+
@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "names"])
15291531
@final
15301532
def set_names(self, names, level=None, inplace: bool = False):
15311533
"""

pandas/tests/indexes/multi/test_get_set.py

+13
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,19 @@ def test_set_names_with_nlevel_1(inplace):
345345
tm.assert_index_equal(result, expected)
346346

347347

348+
def test_multi_set_names_pos_args_deprecation():
349+
# GH#41485
350+
idx = MultiIndex.from_product([["python", "cobra"], [2018, 2019]])
351+
352+
msg = (
353+
"Starting with pandas version 2.0 all arguments of Index.set_names "
354+
"except for the argument 'names' will be keyword-only"
355+
)
356+
357+
with tm.assert_produces_warning(FutureWarning, match=msg):
358+
idx.set_names(["kind", "year"], None)
359+
360+
348361
@pytest.mark.parametrize("ordered", [True, False])
349362
def test_set_levels_categorical(ordered):
350363
# GH13854

pandas/tests/indexes/test_base.py

+13
Original file line numberDiff line numberDiff line change
@@ -1738,3 +1738,16 @@ def test_construct_from_memoryview(klass, extra_kwargs):
17381738
result = klass(memoryview(np.arange(2000, 2005)), **extra_kwargs)
17391739
expected = klass(range(2000, 2005), **extra_kwargs)
17401740
tm.assert_index_equal(result, expected)
1741+
1742+
1743+
def test_index_set_names_pos_args_deprecation():
1744+
# GH#41485
1745+
idx = Index([1, 2, 3, 4])
1746+
1747+
msg = (
1748+
"Starting with pandas version 2.0 all arguments of Index.set_names "
1749+
"except for the argument 'names' will be keyword-only"
1750+
)
1751+
1752+
with tm.assert_produces_warning(FutureWarning, match=msg):
1753+
idx.set_names("quarter", None)

0 commit comments

Comments
 (0)