Skip to content

Commit

Permalink
ENH: Deprecate positional arguments for DataFrame.fillna and Series.f…
Browse files Browse the repository at this point in the history
…illna
  • Loading branch information
jackzyliu committed May 19, 2021
1 parent 896256e commit 275ed38
Show file tree
Hide file tree
Showing 5 changed files with 29 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 @@ -648,6 +648,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 ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (: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 @@ -5166,6 +5167,7 @@ def fillna(
) -> DataFrame | None:
...

@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "value"])
@doc(NDFrame.fillna, **_shared_doc_kwargs)
def fillna(
self,
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from pandas.util._decorators import (
Appender,
Substitution,
deprecate_nonkeyword_arguments,
doc,
)
from pandas.util._validators import (
Expand Down Expand Up @@ -4707,6 +4708,7 @@ def fillna(
...

# error: Cannot determine type of 'fillna'
@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "value"])
@doc(NDFrame.fillna, **_shared_doc_kwargs) # type: ignore[has-type]
def fillna(
self,
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,18 @@ def test_fillna_downcast_dict(self):
expected = DataFrame({"col1": [1, 2]})
tm.assert_frame_equal(result, expected)

def test_fillna_pos_args_deprecation(self):
# https://github.com/pandas-dev/pandas/issues/41485
df = DataFrame({"a": [1, 2, 3, np.nan]}, dtype=float)
msg = (
r"Starting with pandas version 2.0 all arguments of DataFrame.fillna "
r"except for the argument 'value' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.fillna(0, None, None)
expected = DataFrame({"a": [1, 2, 3, 0]}, dtype=float)
tm.assert_frame_equal(result, expected)


def test_fillna_nonconsolidated_frame():
# https://github.com/pandas-dev/pandas/issues/36495
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/series/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,18 @@ def test_fillna_datetime64_with_timezone_tzinfo(self):
expected = Series([ser[0], ts, ser[2]], dtype=object)
tm.assert_series_equal(result, expected)

def test_fillna_pos_args_deprecation(self):
# https://github.com/pandas-dev/pandas/issues/41485
srs = Series([1, 2, 3, np.nan], dtype=float)
msg = (
r"Starting with pandas version 2.0 all arguments of Series.fillna "
r"except for the argument 'value' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = srs.fillna(0, None, None)
expected = Series([1, 2, 3, 0], dtype=float)
tm.assert_series_equal(result, expected)


class TestFillnaPad:
def test_fillna_bug(self):
Expand Down

0 comments on commit 275ed38

Please sign in to comment.