Skip to content

Commit

Permalink
Deprecate passing default arguments as positional in reset_index (#41496
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MarcoGorelli committed May 26, 2021
1 parent b880cf9 commit 45ad3ef
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
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 @@ -684,6 +684,7 @@ Deprecations
- Deprecated passing arguments as positional in :meth:`DataFrame.sort_index` and :meth:`Series.sort_index` (:issue:`41485`)
- 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`)
- 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`)
- Deprecated passing arguments as positional in :meth:`DataFrame.where` and :meth:`Series.where` (other than ``"cond"`` and ``"other"``) (:issue:`41485`)

Expand Down
1 change: 1 addition & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5596,6 +5596,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

0 comments on commit 45ad3ef

Please sign in to comment.