Skip to content

Commit d2bf501

Browse files
authored
CLN: enforce deprecation of the method keyword on df.fillna (#57760)
* enforce deprecation of param method in df.fillna: correct def fillna, fix tests * correct def fillna, fix tests * correct an example in v0.10.0, fix test * add a note to v3.0.0 * remove an entry from ignored_doctest_warnings * fix pylint error in test_sparse.py * correct fillna docstring * correct fillna docstring II * correct tests
1 parent d79910c commit d2bf501

File tree

11 files changed

+86
-204
lines changed

11 files changed

+86
-204
lines changed

doc/source/whatsnew/v0.10.0.rst

+30-6
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,42 @@ labeled the aggregated group with the end of the interval: the next day).
242242
- Calling ``fillna`` on Series or DataFrame with no arguments is no longer
243243
valid code. You must either specify a fill value or an interpolation method:
244244

245-
.. ipython:: python
246-
:okwarning:
245+
.. code-block:: ipython
247246
248-
s = pd.Series([np.nan, 1.0, 2.0, np.nan, 4])
249-
s
250-
s.fillna(0)
251-
s.fillna(method="pad")
247+
In [6]: s = pd.Series([np.nan, 1.0, 2.0, np.nan, 4])
248+
249+
In [7]: s
250+
Out[7]:
251+
0 NaN
252+
1 1.0
253+
2 2.0
254+
3 NaN
255+
4 4.0
256+
dtype: float64
257+
258+
In [8]: s.fillna(0)
259+
Out[8]:
260+
0 0.0
261+
1 1.0
262+
2 2.0
263+
3 0.0
264+
4 4.0
265+
dtype: float64
266+
267+
In [9]: s.fillna(method="pad")
268+
Out[9]:
269+
0 NaN
270+
1 1.0
271+
2 2.0
272+
3 2.0
273+
4 4.0
274+
dtype: float64
252275
253276
Convenience methods ``ffill`` and ``bfill`` have been added:
254277

255278
.. ipython:: python
256279
280+
s = pd.Series([np.nan, 1.0, 2.0, np.nan, 4])
257281
s.ffill()
258282
259283

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ Removal of prior version deprecations/changes
241241
- Removed argument ``limit`` from :meth:`DataFrame.pct_change`, :meth:`Series.pct_change`, :meth:`.DataFrameGroupBy.pct_change`, and :meth:`.SeriesGroupBy.pct_change`; the argument ``method`` must be set to ``None`` and will be removed in a future version of pandas (:issue:`53520`)
242242
- Removed deprecated argument ``obj`` in :meth:`.DataFrameGroupBy.get_group` and :meth:`.SeriesGroupBy.get_group` (:issue:`53545`)
243243
- Removed deprecated behavior of :meth:`Series.agg` using :meth:`Series.apply` (:issue:`53325`)
244+
- Removed deprecated keyword ``method`` on :meth:`Series.fillna`, :meth:`DataFrame.fillna` (:issue:`57760`)
244245
- Removed option ``mode.use_inf_as_na``, convert inf entries to ``NaN`` before instead (:issue:`51684`)
245246
- Removed support for :class:`DataFrame` in :meth:`DataFrame.from_records`(:issue:`51697`)
246247
- Removed support for ``errors="ignore"`` in :func:`to_datetime`, :func:`to_timedelta` and :func:`to_numeric` (:issue:`55734`)

pandas/conftest.py

-4
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,6 @@ def pytest_collection_modifyitems(items, config) -> None:
173173
"DataFrameGroupBy.fillna",
174174
"DataFrameGroupBy.fillna with 'method' is deprecated",
175175
),
176-
(
177-
"DataFrameGroupBy.fillna",
178-
"DataFrame.fillna with 'method' is deprecated",
179-
),
180176
("read_parquet", "Passing a BlockManager to DataFrame is deprecated"),
181177
]
182178

pandas/core/generic.py

+8-37
Original file line numberDiff line numberDiff line change
@@ -6758,7 +6758,6 @@ def fillna(
67586758
self,
67596759
value: Hashable | Mapping | Series | DataFrame = ...,
67606760
*,
6761-
method: FillnaOptions | None = ...,
67626761
axis: Axis | None = ...,
67636762
inplace: Literal[False] = ...,
67646763
limit: int | None = ...,
@@ -6769,7 +6768,6 @@ def fillna(
67696768
self,
67706769
value: Hashable | Mapping | Series | DataFrame = ...,
67716770
*,
6772-
method: FillnaOptions | None = ...,
67736771
axis: Axis | None = ...,
67746772
inplace: Literal[True],
67756773
limit: int | None = ...,
@@ -6780,7 +6778,6 @@ def fillna(
67806778
self,
67816779
value: Hashable | Mapping | Series | DataFrame = ...,
67826780
*,
6783-
method: FillnaOptions | None = ...,
67846781
axis: Axis | None = ...,
67856782
inplace: bool = ...,
67866783
limit: int | None = ...,
@@ -6795,13 +6792,12 @@ def fillna(
67956792
self,
67966793
value: Hashable | Mapping | Series | DataFrame | None = None,
67976794
*,
6798-
method: FillnaOptions | None = None,
67996795
axis: Axis | None = None,
68006796
inplace: bool = False,
68016797
limit: int | None = None,
68026798
) -> Self | None:
68036799
"""
6804-
Fill NA/NaN values using the specified method.
6800+
Fill NA/NaN values with `value`.
68056801
68066802
Parameters
68076803
----------
@@ -6811,15 +6807,6 @@ def fillna(
68116807
each index (for a Series) or column (for a DataFrame). Values not
68126808
in the dict/Series/DataFrame will not be filled. This value cannot
68136809
be a list.
6814-
method : {{'backfill', 'bfill', 'ffill', None}}, default None
6815-
Method to use for filling holes in reindexed Series:
6816-
6817-
* ffill: propagate last valid observation forward to next valid.
6818-
* backfill / bfill: use next valid observation to fill gap.
6819-
6820-
.. deprecated:: 2.1.0
6821-
Use ffill or bfill instead.
6822-
68236810
axis : {axes_single_arg}
68246811
Axis along which to fill missing values. For `Series`
68256812
this parameter is unused and defaults to 0.
@@ -6828,12 +6815,8 @@ def fillna(
68286815
other views on this object (e.g., a no-copy slice for a column in a
68296816
DataFrame).
68306817
limit : int, default None
6831-
If method is specified, this is the maximum number of consecutive
6832-
NaN values to forward/backward fill. In other words, if there is
6833-
a gap with more than this number of consecutive NaNs, it will only
6834-
be partially filled. If method is not specified, this is the
6835-
maximum number of entries along the entire axis where NaNs will be
6836-
filled. Must be greater than 0 if not None.
6818+
This is the maximum number of entries along the entire axis
6819+
where NaNs will be filled. Must be greater than 0 if not None.
68376820
68386821
Returns
68396822
-------
@@ -6918,14 +6901,10 @@ def fillna(
69186901
stacklevel=2,
69196902
)
69206903

6921-
value, method = validate_fillna_kwargs(value, method)
6922-
if method is not None:
6923-
warnings.warn(
6924-
f"{type(self).__name__}.fillna with 'method' is deprecated and "
6925-
"will raise in a future version. Use obj.ffill() or obj.bfill() "
6926-
"instead.",
6927-
FutureWarning,
6928-
stacklevel=find_stack_level(),
6904+
if isinstance(value, (list, tuple)):
6905+
raise TypeError(
6906+
'"value" parameter must be a scalar or dict, but '
6907+
f'you passed a "{type(value).__name__}"'
69296908
)
69306909

69316910
# set the default here, so functions examining the signaure
@@ -6935,15 +6914,7 @@ def fillna(
69356914
axis = self._get_axis_number(axis)
69366915

69376916
if value is None:
6938-
return self._pad_or_backfill(
6939-
# error: Argument 1 to "_pad_or_backfill" of "NDFrame" has
6940-
# incompatible type "Optional[Literal['backfill', 'bfill', 'ffill',
6941-
# 'pad']]"; expected "Literal['ffill', 'bfill', 'pad', 'backfill']"
6942-
method, # type: ignore[arg-type]
6943-
axis=axis,
6944-
limit=limit,
6945-
inplace=inplace,
6946-
)
6917+
raise ValueError("Must specify a fill 'value'.")
69476918
else:
69486919
if self.ndim == 1:
69496920
if isinstance(value, (dict, ABCSeries)):

pandas/tests/extension/base/missing.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ def test_fillna_scalar(self, data_missing):
6868
expected = data_missing.fillna(valid)
6969
tm.assert_extension_array_equal(result, expected)
7070

71-
@pytest.mark.filterwarnings(
72-
"ignore:Series.fillna with 'method' is deprecated:FutureWarning"
73-
)
7471
def test_fillna_limit_pad(self, data_missing):
7572
arr = data_missing.take([1, 0, 0, 0, 1])
7673
result = pd.Series(arr).ffill(limit=2)
@@ -99,12 +96,9 @@ def test_ffill_limit_area(
9996
expected = pd.Series(data_missing.take(expected_ilocs))
10097
tm.assert_series_equal(result, expected)
10198

102-
@pytest.mark.filterwarnings(
103-
"ignore:Series.fillna with 'method' is deprecated:FutureWarning"
104-
)
10599
def test_fillna_limit_backfill(self, data_missing):
106100
arr = data_missing.take([1, 0, 0, 0, 1])
107-
result = pd.Series(arr).fillna(method="backfill", limit=2)
101+
result = pd.Series(arr).bfill(limit=2)
108102
expected = pd.Series(data_missing.take([1, 0, 1, 1, 1]))
109103
tm.assert_series_equal(result, expected)
110104

pandas/tests/extension/decimal/test_decimal.py

-9
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,6 @@ def test_ffill_limit_area(
187187
)
188188

189189
def test_fillna_limit_backfill(self, data_missing):
190-
msg = "Series.fillna with 'method' is deprecated"
191-
with tm.assert_produces_warning(
192-
FutureWarning,
193-
match=msg,
194-
check_stacklevel=False,
195-
raise_on_extra_warnings=False,
196-
):
197-
super().test_fillna_limit_backfill(data_missing)
198-
199190
msg = "ExtensionArray.fillna 'method' keyword is deprecated"
200191
with tm.assert_produces_warning(
201192
DeprecationWarning,

pandas/tests/extension/test_sparse.py

-5
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,6 @@ def test_isna(self, data_missing):
234234
expected = SparseArray([False, False], fill_value=False, dtype=expected_dtype)
235235
tm.assert_equal(sarr.isna(), expected)
236236

237-
def test_fillna_limit_backfill(self, data_missing):
238-
warns = FutureWarning
239-
with tm.assert_produces_warning(warns, check_stacklevel=False):
240-
super().test_fillna_limit_backfill(data_missing)
241-
242237
def test_fillna_no_op_returns_copy(self, data, request):
243238
super().test_fillna_no_op_returns_copy(data)
244239

0 commit comments

Comments
 (0)