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

Deprecate passing default arguments as positional in reset_index #41496

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -677,6 +677,7 @@ Deprecations
- 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`)
- Deprecated passing arguments as positional in :meth:`DataFrame.reset_index` (other than ``"level"``) and :meth:`Series.reset_index` (:issue:`41485`)
- Deprecated construction of :class:`Series` or :class:`DataFrame` with ``DatetimeTZDtype`` data and ``datetime64[ns]`` dtype. Use ``Series(data).dt.tz_localize(None)`` instead (:issue:`41555`,:issue:`33401`)

.. _whatsnew_130.deprecations.nuisance_columns:
Expand Down
1 change: 1 addition & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5619,6 +5619,7 @@ def reset_index(
) -> DataFrame | None:
...

@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "level"])
def reset_index(
self,
level: Hashable | Sequence[Hashable] | None = None,
Expand Down
1 change: 1 addition & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,7 @@ def repeat(self, repeats, axis=None) -> Series:
self, method="repeat"
)

@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "level"])
def reset_index(self, level=None, drop=False, name=None, inplace=False):
"""
Generate a new DataFrame or Series with the index reset.
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/frame/methods/test_reset_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,16 @@ def test_reset_index_multiindex_nat():
index=pd.DatetimeIndex(["2015-07-01", "2015-07-02", "NaT"], name="tstamp"),
)
tm.assert_frame_equal(result, expected)


def test_drop_pos_args_deprecation():
# https://github.com/pandas-dev/pandas/issues/41485
df = DataFrame({"a": [1, 2, 3]}).set_index("a")
msg = (
r"In a future version of pandas all arguments of DataFrame\.reset_index "
r"except for the argument 'level' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.reset_index("a", False)
expected = DataFrame({"a": [1, 2, 3]})
tm.assert_frame_equal(result, expected)
12 changes: 12 additions & 0 deletions pandas/tests/series/methods/test_reset_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ def test_reset_index_with_drop(self, series_with_multilevel_index):
assert isinstance(deleveled, Series)
assert deleveled.index.name == ser.index.name

def test_drop_pos_args_deprecation(self):
# https://github.com/pandas-dev/pandas/issues/41485
ser = Series([1, 2, 3], index=Index([1, 2, 3], name="a"))
msg = (
r"In a future version of pandas all arguments of Series\.reset_index "
r"except for the argument 'level' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = ser.reset_index("a", False)
expected = DataFrame({"a": [1, 2, 3], 0: [1, 2, 3]})
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"array, dtype",
Expand Down