Skip to content

Commit 44dc8c3

Browse files
jmholzerTLouf
authored andcommitted
ENH: Deprecate non-keyword arguments for Index.set_names. (pandas-dev#41551)
1 parent 1204aca commit 44dc8c3

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ Deprecations
679679
- Deprecated the ``convert_float`` optional argument in :func:`read_excel` and :meth:`ExcelFile.parse` (:issue:`41127`)
680680
- 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`)
681681
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
682+
- Deprecated passing arguments as positional in :meth:`Index.set_names` and :meth:`MultiIndex.set_names` (except for ``names``) (:issue:`41485`)
682683
- Deprecated passing arguments as positional in :meth:`DataFrame.clip` and :meth:`Series.clip` (other than ``"upper"`` and ``"lower"``) (:issue:`41485`)
683684
- Deprecated special treatment of lists with first element a Categorical in the :class:`DataFrame` constructor; pass as ``pd.DataFrame({col: categorical, ...})`` instead (:issue:`38845`)
684685
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`)

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ def _set_names(self, values, level=None) -> None:
15381538

15391539
names = property(fset=_set_names, fget=_get_names)
15401540

1541-
@final
1541+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "names"])
15421542
def set_names(self, names, level=None, inplace: bool = False):
15431543
"""
15441544
Set Index or MultiIndex name.

pandas/core/indexes/multi.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ class MultiIndex(Index):
292292
_levels = FrozenList()
293293
_codes = FrozenList()
294294
_comparables = ["names"]
295-
rename = Index.set_names
296295

297296
sortorder: int | None
298297

@@ -3585,7 +3584,9 @@ def _get_reconciled_name_object(self, other) -> MultiIndex:
35853584
"""
35863585
names = self._maybe_match_names(other)
35873586
if self.names != names:
3588-
return self.rename(names)
3587+
# Incompatible return value type (got "Optional[MultiIndex]", expected
3588+
# "MultiIndex")
3589+
return self.rename(names) # type: ignore[return-value]
35893590
return self
35903591

35913592
def _maybe_match_names(self, other):
@@ -3784,6 +3785,12 @@ def isin(self, values, level=None) -> np.ndarray:
37843785
return np.zeros(len(levs), dtype=np.bool_)
37853786
return levs.isin(values)
37863787

3788+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "names"])
3789+
def set_names(self, names, level=None, inplace: bool = False) -> MultiIndex | None:
3790+
return super().set_names(names=names, level=level, inplace=inplace)
3791+
3792+
rename = set_names
3793+
37873794
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
37883795
def drop_duplicates(self, keep: str | bool = "first") -> MultiIndex:
37893796
return super().drop_duplicates(keep=keep)

pandas/tests/indexes/multi/test_get_set.py

+17
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,23 @@ 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+
msg = (
352+
"In a future version of pandas all arguments of MultiIndex.set_names "
353+
"except for the argument 'names' will be keyword-only"
354+
)
355+
with tm.assert_produces_warning(FutureWarning, match=msg):
356+
result = idx.set_names(["kind", "year"], None)
357+
expected = MultiIndex(
358+
levels=[["python", "cobra"], [2018, 2019]],
359+
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
360+
names=["kind", "year"],
361+
)
362+
tm.assert_index_equal(result, expected)
363+
364+
348365
@pytest.mark.parametrize("ordered", [True, False])
349366
def test_set_levels_categorical(ordered):
350367
# GH13854

pandas/tests/indexes/test_base.py

+13
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,19 @@ def test_construct_from_memoryview(klass, extra_kwargs):
17401740
tm.assert_index_equal(result, expected)
17411741

17421742

1743+
def test_index_set_names_pos_args_deprecation():
1744+
# GH#41485
1745+
idx = Index([1, 2, 3, 4])
1746+
msg = (
1747+
"In a future version of pandas all arguments of Index.set_names "
1748+
"except for the argument 'names' will be keyword-only"
1749+
)
1750+
with tm.assert_produces_warning(FutureWarning, match=msg):
1751+
result = idx.set_names("quarter", None)
1752+
expected = Index([1, 2, 3, 4], name="quarter")
1753+
tm.assert_index_equal(result, expected)
1754+
1755+
17431756
def test_drop_duplicates_pos_args_deprecation():
17441757
# GH#41485
17451758
idx = Index([1, 2, 3, 1])

0 commit comments

Comments
 (0)