Skip to content

Commit

Permalink
ENH: Deprecate non-keyword arguments for Index.set_names.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmholzer committed May 25, 2021
1 parent 17f5ff0 commit fc525c8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
1 change: 0 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,6 @@ def _set_names(self, values, level=None) -> None:
names = property(fset=_set_names, fget=_get_names)

@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "names"])
@final
def set_names(self, names, level=None, inplace: bool = False):
"""
Set Index or MultiIndex name.
Expand Down
11 changes: 8 additions & 3 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from pandas.util._decorators import (
Appender,
cache_readonly,
deprecate_nonkeyword_arguments,
doc,
)

Expand Down Expand Up @@ -295,7 +296,6 @@ class MultiIndex(Index):
_levels = FrozenList()
_codes = FrozenList()
_comparables = ["names"]
rename = Index.set_names

sortorder: int | None

Expand Down Expand Up @@ -3780,19 +3780,24 @@ def delete(self, loc) -> MultiIndex:
verify_integrity=False,
)

@doc(Index.isin)
# @doc(Index.isin)
def isin(self, values, level=None) -> np.ndarray:
if level is None:
values = MultiIndex.from_tuples(values, names=self.names)._values
return algos.isin(self._values, values)
else:
num = self._get_level_number(level)
levs = self.get_level_values(num)

if levs.size == 0:
return np.zeros(len(levs), dtype=np.bool_)
return levs.isin(values)

@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "names"])
def set_names(self, names, level=None, inplace: bool = False) -> MultiIndex | None:
return super().set_names(names=names, level=level, inplace=inplace)

rename = set_names

# ---------------------------------------------------------------
# Arithmetic/Numeric Methods - Disabled

Expand Down
6 changes: 1 addition & 5 deletions pandas/tests/indexes/multi/test_get_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,21 +348,17 @@ def test_set_names_with_nlevel_1(inplace):
def test_multi_set_names_pos_args_deprecation():
# GH#41485
idx = MultiIndex.from_product([["python", "cobra"], [2018, 2019]])

msg = (
"In a future version of pandas all arguments of Index.set_names "
"In a future version of pandas all arguments of MultiIndex.set_names "
"except for the argument 'names' will be keyword-only"
)

with tm.assert_produces_warning(FutureWarning, match=msg):
result = idx.set_names(["kind", "year"], None)

expected = MultiIndex(
levels=[["python", "cobra"], [2018, 2019]],
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
names=["kind", "year"],
)

tm.assert_index_equal(result, expected)


Expand Down

0 comments on commit fc525c8

Please sign in to comment.