Skip to content

Commit 45ad3ef

Browse files
authored
Deprecate passing default arguments as positional in reset_index (#41496)
1 parent b880cf9 commit 45ad3ef

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ Deprecations
684684
- Deprecated passing arguments as positional in :meth:`DataFrame.sort_index` and :meth:`Series.sort_index` (:issue:`41485`)
685685
- Deprecated passing arguments as positional in :meth:`DataFrame.drop_duplicates` (except for ``subset``), :meth:`Series.drop_duplicates`, :meth:`Index.drop_duplicates` and :meth:`MultiIndex.drop_duplicates`(:issue:`41485`)
686686
- Deprecated passing arguments (apart from ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (:issue:`41485`)
687+
- Deprecated passing arguments as positional in :meth:`DataFrame.reset_index` (other than ``"level"``) and :meth:`Series.reset_index` (:issue:`41485`)
687688
- 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`)
688689
- Deprecated passing arguments as positional in :meth:`DataFrame.where` and :meth:`Series.where` (other than ``"cond"`` and ``"other"``) (:issue:`41485`)
689690

pandas/core/frame.py

+1
Original file line numberDiff line numberDiff line change
@@ -5596,6 +5596,7 @@ def reset_index(
55965596
) -> DataFrame | None:
55975597
...
55985598

5599+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "level"])
55995600
def reset_index(
56005601
self,
56015602
level: Hashable | Sequence[Hashable] | None = None,

pandas/core/series.py

+1
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,7 @@ def repeat(self, repeats, axis=None) -> Series:
13081308
self, method="repeat"
13091309
)
13101310

1311+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "level"])
13111312
def reset_index(self, level=None, drop=False, name=None, inplace=False):
13121313
"""
13131314
Generate a new DataFrame or Series with the index reset.

pandas/tests/frame/methods/test_reset_index.py

+13
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,16 @@ def test_reset_index_multiindex_nat():
671671
index=pd.DatetimeIndex(["2015-07-01", "2015-07-02", "NaT"], name="tstamp"),
672672
)
673673
tm.assert_frame_equal(result, expected)
674+
675+
676+
def test_drop_pos_args_deprecation():
677+
# https://github.com/pandas-dev/pandas/issues/41485
678+
df = DataFrame({"a": [1, 2, 3]}).set_index("a")
679+
msg = (
680+
r"In a future version of pandas all arguments of DataFrame\.reset_index "
681+
r"except for the argument 'level' will be keyword-only"
682+
)
683+
with tm.assert_produces_warning(FutureWarning, match=msg):
684+
result = df.reset_index("a", False)
685+
expected = DataFrame({"a": [1, 2, 3]})
686+
tm.assert_frame_equal(result, expected)

pandas/tests/series/methods/test_reset_index.py

+12
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ def test_reset_index_with_drop(self, series_with_multilevel_index):
148148
assert isinstance(deleveled, Series)
149149
assert deleveled.index.name == ser.index.name
150150

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

152164
@pytest.mark.parametrize(
153165
"array, dtype",

0 commit comments

Comments
 (0)