Skip to content
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

ENH: Deprecate non-keyword arguments for Index.set_names. #41551

Merged
merged 11 commits into from
May 27, 2021
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ Deprecations
- Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`)
- 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`)
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
- Deprecated passing arguments as positional in :meth:`Index.set_names` (except for ``names``) (:issue:`41485`)
- Deprecated special treatment of lists with first element a Categorical in the :class:`DataFrame` constructor; pass as ``pd.DataFrame({col: categorical, ...})`` instead (:issue:`38845`)
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`)
- Deprecated passing arguments (apart from ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (:issue:`41485`)
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from pandas.util._decorators import (
Appender,
cache_readonly,
deprecate_nonkeyword_arguments,
doc,
)

Expand Down Expand Up @@ -1529,6 +1530,7 @@ 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):
"""
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/indexes/multi/test_get_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,27 @@ def test_set_names_with_nlevel_1(inplace):
tm.assert_index_equal(result, expected)


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 "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as in the other PR, you'll probably need to define this for MultiIndex too (and remove @final) so that the warning shows MultiIndex.set_names

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this change in fc525c8.

"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)


@pytest.mark.parametrize("ordered", [True, False])
def test_set_levels_categorical(ordered):
# GH13854
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1738,3 +1738,20 @@ def test_construct_from_memoryview(klass, extra_kwargs):
result = klass(memoryview(np.arange(2000, 2005)), **extra_kwargs)
expected = klass(range(2000, 2005), **extra_kwargs)
tm.assert_index_equal(result, expected)


def test_index_set_names_pos_args_deprecation():
# GH#41485
idx = Index([1, 2, 3, 4])

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

with tm.assert_produces_warning(FutureWarning, match=msg):
result = idx.set_names("quarter", None)

expected = Index([1, 2, 3, 4], name="quarter")

tm.assert_index_equal(result, expected)