Skip to content

DEPR: inplace in GroupBy.fillna #53438

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

Closed
Closed
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ Deprecations
- Deprecated behavior of :func:`assert_series_equal` and :func:`assert_frame_equal` considering NA-like values (e.g. ``NaN`` vs ``None`` as equivalent) (:issue:`52081`)
- Deprecated constructing :class:`SparseArray` from scalar data, pass a sequence instead (:issue:`53039`)
- Deprecated positional indexing on :class:`Series` with :meth:`Series.__getitem__` and :meth:`Series.__setitem__`, in a future version ``ser[item]`` will *always* interpret ``item`` as a label, not a position (:issue:`50617`)
-
- Deprecated the ``inplace`` argument of :class:`SeriesGroupBy.fillna` and :class:`DataFrameGroupBy.fillna`; setting ``inplace=True`` is currently broken (:issue:`48792`)

.. ---------------------------------------------------------------------------
.. _whatsnew_210.performance:
Expand Down
34 changes: 32 additions & 2 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ def fillna(
value: object | ArrayLike | None = None,
method: FillnaOptions | None = None,
axis: Axis | None | lib.NoDefault = lib.no_default,
inplace: bool = False,
inplace: bool | lib.NoDefault = lib.no_default,
limit: int | None = None,
downcast: dict | None = None,
) -> Series | None:
Expand Down Expand Up @@ -860,6 +860,10 @@ def fillna(

inplace : bool, default False
Broken. Do not set to True.

.. deprecated:: 2.1.0
The inplace argument will be removed in a future version of pandas.

limit : int, default None
If method is specified, this is the maximum number of consecutive
NaN values to forward/backward fill within a group. In other words,
Expand Down Expand Up @@ -925,6 +929,17 @@ def fillna(
5 NaN
dtype: float64
"""
if inplace is not lib.no_default:
warnings.warn(
message=(
"Setting inplace=True is currently broken. The inplace argument "
"is deprecated and will be removed in a future version of pandas."
),
category=FutureWarning,
stacklevel=find_stack_level(),
)
else:
inplace = False
result = self._op_via_apply(
"fillna",
value=value,
Expand Down Expand Up @@ -2353,7 +2368,7 @@ def fillna(
value: Hashable | Mapping | Series | DataFrame = None,
method: FillnaOptions | None = None,
axis: Axis | None | lib.NoDefault = lib.no_default,
inplace: bool = False,
inplace: bool | lib.NoDefault = lib.no_default,
limit: int | None = None,
downcast=None,
) -> DataFrame | None:
Expand Down Expand Up @@ -2387,6 +2402,10 @@ def fillna(

inplace : bool, default False
Broken. Do not set to True.

.. deprecated:: 2.1.0
The inplace argument will be removed in a future version of pandas.

limit : int, default None
If method is specified, this is the maximum number of consecutive
NaN values to forward/backward fill within a group. In other words,
Expand Down Expand Up @@ -2473,6 +2492,17 @@ def fillna(
3 3.0 NaN 2.0
4 3.0 NaN NaN
"""
if inplace is not lib.no_default:
warnings.warn(
message=(
"Setting inplace=True is currently broken. The inplace argument "
"is deprecated and will be removed in a future version of pandas."
),
category=FutureWarning,
stacklevel=find_stack_level(),
)
else:
inplace = False
result = self._op_via_apply(
"fillna",
value=value,
Expand Down
35 changes: 35 additions & 0 deletions pandas/tests/groupby/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -1719,3 +1719,38 @@ def test_regression_allowlist_methods(op, axis, skipna, sort):
if sort:
expected = expected.sort_index(axis=axis)
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("inplace", [True, False, lib.no_default])
def test_fillna_inplace_depr_frame(inplace):
# GH#48792
df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5]})
gb = df.groupby("a")
if inplace is lib.no_default:
result = gb.fillna(method="ffill", inplace=inplace)
else:
with tm.assert_produces_warning(
FutureWarning, match="The inplace argument is deprecated"
):
result = gb.fillna(method="ffill", inplace=inplace)
expected = DataFrame() if inplace and inplace is not lib.no_default else df[["b"]]
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("inplace", [True, False, lib.no_default])
def test_fillna_inplace_depr_series(inplace):
# GH#48792
ser = Series([3, 4, 5])
gb = ser.groupby([1, 1, 2])
if inplace is lib.no_default:
result = gb.fillna(method="ffill", inplace=inplace)
else:
with tm.assert_produces_warning(
FutureWarning, match="The inplace argument is deprecated"
):
result = gb.fillna(method="ffill", inplace=inplace)
if inplace and inplace is not lib.no_default:
expected = Series([None, None], dtype=object, index=[1, 2])
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 you need to specify index dtype for windows/32bit builds

else:
expected = ser
tm.assert_series_equal(result, expected)