Skip to content

Commit 075fcbd

Browse files
MarcoGorelliJulianWgs
authored andcommitted
Deprecate passing args as positional DataFrame/Series.ffill (pandas-dev#41508)
1 parent d8ccd5d commit 075fcbd

File tree

6 files changed

+89
-82
lines changed

6 files changed

+89
-82
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ Deprecations
682682
- Deprecated passing arguments as positional in :meth:`DataFrame.clip` and :meth:`Series.clip` (other than ``"upper"`` and ``"lower"``) (:issue:`41485`)
683683
- Deprecated special treatment of lists with first element a Categorical in the :class:`DataFrame` constructor; pass as ``pd.DataFrame({col: categorical, ...})`` instead (:issue:`38845`)
684684
- Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`)
685+
- Deprecated passing arguments as positional in :meth:`DataFrame.ffill`, :meth:`Series.ffill`, :meth:`DataFrame.bfill`, and :meth:`Series.bfill` (:issue:`41485`)
685686
- Deprecated passing arguments as positional in :meth:`DataFrame.sort_values` (other than ``"by"``) and :meth:`Series.sort_values` (:issue:`41485`)
686687
- Deprecated passing arguments as positional in :meth:`DataFrame.dropna` and :meth:`Series.dropna` (:issue:`41485`)
687688
- Deprecated passing arguments as positional in :meth:`DataFrame.set_index` (other than ``"keys"``) (:issue:`41485`)

pandas/core/frame.py

+20
Original file line numberDiff line numberDiff line change
@@ -10646,6 +10646,26 @@ def values(self) -> np.ndarray:
1064610646
self._consolidate_inplace()
1064710647
return self._mgr.as_array(transpose=True)
1064810648

10649+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
10650+
def ffill(
10651+
self: DataFrame,
10652+
axis: None | Axis = None,
10653+
inplace: bool = False,
10654+
limit: None | int = None,
10655+
downcast=None,
10656+
) -> DataFrame | None:
10657+
return super().ffill(axis, inplace, limit, downcast)
10658+
10659+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
10660+
def bfill(
10661+
self: DataFrame,
10662+
axis: None | Axis = None,
10663+
inplace: bool = False,
10664+
limit: None | int = None,
10665+
downcast=None,
10666+
) -> DataFrame | None:
10667+
return super().bfill(axis, inplace, limit, downcast)
10668+
1064910669
@deprecate_nonkeyword_arguments(
1065010670
version=None, allowed_args=["self", "lower", "upper"]
1065110671
)

pandas/core/generic.py

-82
Original file line numberDiff line numberDiff line change
@@ -6392,47 +6392,6 @@ def fillna(
63926392
else:
63936393
return result.__finalize__(self, method="fillna")
63946394

6395-
@overload
6396-
def ffill(
6397-
self: FrameOrSeries,
6398-
axis: None | Axis = ...,
6399-
inplace: Literal[False] = ...,
6400-
limit: None | int = ...,
6401-
downcast=...,
6402-
) -> FrameOrSeries:
6403-
...
6404-
6405-
@overload
6406-
def ffill(
6407-
self: FrameOrSeries,
6408-
axis: None | Axis,
6409-
inplace: Literal[True],
6410-
limit: None | int = ...,
6411-
downcast=...,
6412-
) -> None:
6413-
...
6414-
6415-
@overload
6416-
def ffill(
6417-
self: FrameOrSeries,
6418-
*,
6419-
inplace: Literal[True],
6420-
limit: None | int = ...,
6421-
downcast=...,
6422-
) -> None:
6423-
...
6424-
6425-
@overload
6426-
def ffill(
6427-
self: FrameOrSeries,
6428-
axis: None | Axis = ...,
6429-
inplace: bool_t = ...,
6430-
limit: None | int = ...,
6431-
downcast=...,
6432-
) -> FrameOrSeries | None:
6433-
...
6434-
6435-
@final
64366395
@doc(klass=_shared_doc_kwargs["klass"])
64376396
def ffill(
64386397
self: FrameOrSeries,
@@ -6455,47 +6414,6 @@ def ffill(
64556414

64566415
pad = ffill
64576416

6458-
@overload
6459-
def bfill(
6460-
self: FrameOrSeries,
6461-
axis: None | Axis = ...,
6462-
inplace: Literal[False] = ...,
6463-
limit: None | int = ...,
6464-
downcast=...,
6465-
) -> FrameOrSeries:
6466-
...
6467-
6468-
@overload
6469-
def bfill(
6470-
self: FrameOrSeries,
6471-
axis: None | Axis,
6472-
inplace: Literal[True],
6473-
limit: None | int = ...,
6474-
downcast=...,
6475-
) -> None:
6476-
...
6477-
6478-
@overload
6479-
def bfill(
6480-
self: FrameOrSeries,
6481-
*,
6482-
inplace: Literal[True],
6483-
limit: None | int = ...,
6484-
downcast=...,
6485-
) -> None:
6486-
...
6487-
6488-
@overload
6489-
def bfill(
6490-
self: FrameOrSeries,
6491-
axis: None | Axis = ...,
6492-
inplace: bool_t = ...,
6493-
limit: None | int = ...,
6494-
downcast=...,
6495-
) -> FrameOrSeries | None:
6496-
...
6497-
6498-
@final
64996417
@doc(klass=_shared_doc_kwargs["klass"])
65006418
def bfill(
65016419
self: FrameOrSeries,

pandas/core/series.py

+20
Original file line numberDiff line numberDiff line change
@@ -5292,6 +5292,26 @@ def to_period(self, freq=None, copy=True) -> Series:
52925292
self, method="to_period"
52935293
)
52945294

5295+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
5296+
def ffill(
5297+
self: Series,
5298+
axis: None | Axis = None,
5299+
inplace: bool = False,
5300+
limit: None | int = None,
5301+
downcast=None,
5302+
) -> Series | None:
5303+
return super().ffill(axis, inplace, limit, downcast)
5304+
5305+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
5306+
def bfill(
5307+
self: Series,
5308+
axis: None | Axis = None,
5309+
inplace: bool = False,
5310+
limit: None | int = None,
5311+
downcast=None,
5312+
) -> Series | None:
5313+
return super().bfill(axis, inplace, limit, downcast)
5314+
52955315
@deprecate_nonkeyword_arguments(
52965316
version=None, allowed_args=["self", "lower", "upper"]
52975317
)

pandas/tests/frame/methods/test_fillna.py

+24
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,18 @@ def test_ffill(self, datetime_frame):
326326
datetime_frame.ffill(), datetime_frame.fillna(method="ffill")
327327
)
328328

329+
def test_ffill_pos_args_deprecation(self):
330+
# https://github.com/pandas-dev/pandas/issues/41485
331+
df = DataFrame({"a": [1, 2, 3]})
332+
msg = (
333+
r"In a future version of pandas all arguments of DataFrame.ffill "
334+
r"will be keyword-only"
335+
)
336+
with tm.assert_produces_warning(FutureWarning, match=msg):
337+
result = df.ffill(0)
338+
expected = DataFrame({"a": [1, 2, 3]})
339+
tm.assert_frame_equal(result, expected)
340+
329341
def test_bfill(self, datetime_frame):
330342
datetime_frame["A"][:5] = np.nan
331343
datetime_frame["A"][-5:] = np.nan
@@ -334,6 +346,18 @@ def test_bfill(self, datetime_frame):
334346
datetime_frame.bfill(), datetime_frame.fillna(method="bfill")
335347
)
336348

349+
def test_bfill_pos_args_deprecation(self):
350+
# https://github.com/pandas-dev/pandas/issues/41485
351+
df = DataFrame({"a": [1, 2, 3]})
352+
msg = (
353+
r"In a future version of pandas all arguments of DataFrame.bfill "
354+
r"will be keyword-only"
355+
)
356+
with tm.assert_produces_warning(FutureWarning, match=msg):
357+
result = df.bfill(0)
358+
expected = DataFrame({"a": [1, 2, 3]})
359+
tm.assert_frame_equal(result, expected)
360+
337361
def test_frame_pad_backfill_limit(self):
338362
index = np.arange(10)
339363
df = DataFrame(np.random.randn(10, 4), index=index)

pandas/tests/series/methods/test_fillna.py

+24
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,18 @@ def test_ffill(self):
777777
ts[2] = np.NaN
778778
tm.assert_series_equal(ts.ffill(), ts.fillna(method="ffill"))
779779

780+
def test_ffill_pos_args_deprecation(self):
781+
# https://github.com/pandas-dev/pandas/issues/41485
782+
ser = Series([1, 2, 3])
783+
msg = (
784+
r"In a future version of pandas all arguments of Series.ffill "
785+
r"will be keyword-only"
786+
)
787+
with tm.assert_produces_warning(FutureWarning, match=msg):
788+
result = ser.ffill(0)
789+
expected = Series([1, 2, 3])
790+
tm.assert_series_equal(result, expected)
791+
780792
def test_ffill_mixed_dtypes_without_missing_data(self):
781793
# GH#14956
782794
series = Series([datetime(2015, 1, 1, tzinfo=pytz.utc), 1])
@@ -788,6 +800,18 @@ def test_bfill(self):
788800
ts[2] = np.NaN
789801
tm.assert_series_equal(ts.bfill(), ts.fillna(method="bfill"))
790802

803+
def test_bfill_pos_args_deprecation(self):
804+
# https://github.com/pandas-dev/pandas/issues/41485
805+
ser = Series([1, 2, 3])
806+
msg = (
807+
r"In a future version of pandas all arguments of Series.bfill "
808+
r"will be keyword-only"
809+
)
810+
with tm.assert_produces_warning(FutureWarning, match=msg):
811+
result = ser.bfill(0)
812+
expected = Series([1, 2, 3])
813+
tm.assert_series_equal(result, expected)
814+
791815
def test_pad_nan(self):
792816
x = Series(
793817
[np.nan, 1.0, np.nan, 3.0, np.nan], ["z", "a", "b", "c", "d"], dtype=float

0 commit comments

Comments
 (0)