Skip to content

Deprecate passing default args as positional in DataFrame.set_index #41495

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -647,6 +647,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 (apart from ``keys``) as positional in :meth:`DataFrame.set_index` (:issue:`41485`)

.. ---------------------------------------------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
Appender,
Substitution,
deprecate_kwarg,
deprecate_nonkeyword_arguments,
doc,
rewrite_axis_style_signature,
)
Expand Down Expand Up @@ -5301,6 +5302,7 @@ def shift(
periods=periods, freq=freq, axis=axis, fill_value=fill_value
)

@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "keys"])
Copy link
Contributor

Choose a reason for hiding this comment

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

do we set the version in other deprecations? I don't think we generally want to, for example what if we delayed this to 3.0 then we'd have to update all of these messages.

Copy link
Member

Choose a reason for hiding this comment

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

(I think in general it is useful information to mention the version when it is going to be removed)

Copy link
Member

Choose a reason for hiding this comment

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

what our deprecation policy does say

Deprecations will only be enforced in major releases. For example, if a behavior is deprecated in pandas 1.2.0, it will continue to work, with a warning, for all releases in the 1.x series. The behavior will change and the deprecation removed in the next major release (2.0.0).

out deprecation policy also says

pandas will introduce deprecations in minor releases.

what our deprecation policy does NOT say is what happens when the deprecation is introduced late in the series and there are no further releases in the series.

so if our next release after 1.3 (where these deprecations are introduced) is 2.0, do we deprecate the behaviour? "Deprecations will only be enforced in major releases." so the next major release is 3.0.

if the next release is 1.4, then OK to deprecate in 2.0?

Copy link
Member Author

Choose a reason for hiding this comment

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

I would've thought it would be OK to deprecate in 2.0 regardless of whether there's a 1.4 release

If the deprecation is introduced in 1.3.0 (minor release) and then enforced in 2.0 (the next major release), then isn't that inline with the policy?

I don't understand how you got to "so the next major release is 3.0"

Copy link
Contributor

Choose a reason for hiding this comment

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

this is yet another reason we don't want to actually use the version string in the message

in any event we can choose or not to release 1.4 (no depreciations removed ) or just do 2.0 (where all depreciations are removed)

Copy link
Member

Choose a reason for hiding this comment

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

I recall a comment from Joris that we should not expect all users to upgrade to every version and that users could skip versions.

so if we introduce in one version, should we enforce in the next?

def set_index(
self,
keys,
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/methods/test_set_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,3 +705,13 @@ def test_set_index_periodindex(self):
tm.assert_index_equal(df.index, idx1)
df = df.set_index(idx2)
tm.assert_index_equal(df.index, idx2)

def test_drop_pos_args_deprecation(self):
# https://github.com/pandas-dev/pandas/issues/41485
df = DataFrame({"a": [1, 2, 3]})
msg = (
r"Starting with Pandas version 2\.0 all arguments of set_index except for "
Copy link
Contributor

Choose a reason for hiding this comment

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

and the comment on the other PRs about the message.

r"the arguments 'self' and 'keys' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
df.set_index("a", True)