Skip to content

Commit 04d0e48

Browse files
authored
ENH: Deprecate positional arguments for DataFrame.fillna and Series.fillna (GH41485) (#41559)
Co-authored-by: Jack Liu <[email protected]>
1 parent 16b626a commit 04d0e48

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ Deprecations
674674
- 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`)
675675
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
676676
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`)
677+
- Deprecated passing arguments (apart from ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (:issue:`41485`)
677678

678679
.. ---------------------------------------------------------------------------
679680

pandas/core/frame.py

+1
Original file line numberDiff line numberDiff line change
@@ -5179,6 +5179,7 @@ def fillna(
51795179
) -> DataFrame | None:
51805180
...
51815181

5182+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "value"])
51825183
@doc(NDFrame.fillna, **_shared_doc_kwargs)
51835184
def fillna(
51845185
self,

pandas/core/series.py

+1
Original file line numberDiff line numberDiff line change
@@ -4708,6 +4708,7 @@ def fillna(
47084708
...
47094709

47104710
# error: Cannot determine type of 'fillna'
4711+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "value"])
47114712
@doc(NDFrame.fillna, **_shared_doc_kwargs) # type: ignore[has-type]
47124713
def fillna(
47134714
self,

pandas/tests/frame/methods/test_fillna.py

+12
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,18 @@ def test_fillna_downcast_dict(self):
538538
expected = DataFrame({"col1": [1, 2]})
539539
tm.assert_frame_equal(result, expected)
540540

541+
def test_fillna_pos_args_deprecation(self):
542+
# https://github.com/pandas-dev/pandas/issues/41485
543+
df = DataFrame({"a": [1, 2, 3, np.nan]}, dtype=float)
544+
msg = (
545+
r"In a future version of pandas all arguments of DataFrame.fillna "
546+
r"except for the argument 'value' will be keyword-only"
547+
)
548+
with tm.assert_produces_warning(FutureWarning, match=msg):
549+
result = df.fillna(0, None, None)
550+
expected = DataFrame({"a": [1, 2, 3, 0]}, dtype=float)
551+
tm.assert_frame_equal(result, expected)
552+
541553

542554
def test_fillna_nonconsolidated_frame():
543555
# https://github.com/pandas-dev/pandas/issues/36495

pandas/tests/series/methods/test_fillna.py

+12
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,18 @@ def test_fillna_datetime64_with_timezone_tzinfo(self):
748748
expected = Series([ser[0], ts, ser[2]], dtype=object)
749749
tm.assert_series_equal(result, expected)
750750

751+
def test_fillna_pos_args_deprecation(self):
752+
# https://github.com/pandas-dev/pandas/issues/41485
753+
srs = Series([1, 2, 3, np.nan], dtype=float)
754+
msg = (
755+
r"In a future version of pandas all arguments of Series.fillna "
756+
r"except for the argument 'value' will be keyword-only"
757+
)
758+
with tm.assert_produces_warning(FutureWarning, match=msg):
759+
result = srs.fillna(0, None, None)
760+
expected = Series([1, 2, 3, 0], dtype=float)
761+
tm.assert_series_equal(result, expected)
762+
751763

752764
class TestFillnaPad:
753765
def test_fillna_bug(self):

0 commit comments

Comments
 (0)