From f405a77a7a566a4220e49338e0cf47514acfa292 Mon Sep 17 00:00:00 2001 From: pckSF Date: Sun, 25 Apr 2021 16:48:49 +0100 Subject: [PATCH 1/3] TYP: Overload series/ffill and bfill --- pandas/core/generic.py | 76 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index c77a3717c4c03..7d630b9c80f53 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6469,15 +6469,45 @@ def fillna( else: return result.__finalize__(self, method="fillna") + @overload + def ffill( + self: FrameOrSeries, + axis: None | str | int = ..., + inplace: Literal[False] = ..., + limit: None | int = ..., + downcast: None | str | dict = ..., + ) -> FrameOrSeries: + ... + + @overload + def ffill( + self: FrameOrSeries, + axis: None | str | int = ..., + inplace: Literal[True] = ..., + limit: None | int = ..., + downcast: None | str | dict = ..., + ) -> None: + ... + + @overload + def ffill( + self: FrameOrSeries, + axis: None | str | int = ..., + inplace: bool_t = ..., + limit: None | int = ..., + downcast: None | str | dict = ..., + ) -> FrameOrSeries | None: + ... + @final @doc(klass=_shared_doc_kwargs["klass"]) def ffill( self: FrameOrSeries, - axis=None, + axis: None | str | int = None, inplace: bool_t = False, - limit=None, - downcast=None, - ) -> FrameOrSeries | None: + limit: None | int = None, + downcast: None | str | dict = None, + ): """ Synonym for :meth:`DataFrame.fillna` with ``method='ffill'``. @@ -6492,15 +6522,45 @@ def ffill( pad = ffill + @overload + def bfill( + self: FrameOrSeries, + axis: None | str | int = ..., + inplace: Literal[False] = ..., + limit: None | int = ..., + downcast: None | str | dict = ..., + ) -> FrameOrSeries: + ... + + @overload + def bfill( + self: FrameOrSeries, + axis: None | str | int = ..., + inplace: Literal[True] = ..., + limit: None | int = ..., + downcast: None | str | dict = ..., + ) -> None: + ... + + @overload + def bfill( + self: FrameOrSeries, + axis: None | str | int = ..., + inplace: bool_t = ..., + limit: None | int = ..., + downcast: None | str | dict = ..., + ) -> FrameOrSeries | None: + ... + @final @doc(klass=_shared_doc_kwargs["klass"]) def bfill( self: FrameOrSeries, - axis=None, + axis: None | str | int = None, inplace: bool_t = False, - limit=None, - downcast=None, - ) -> FrameOrSeries | None: + limit: None | int = None, + downcast: None | str | dict = None, + ): """ Synonym for :meth:`DataFrame.fillna` with ``method='bfill'``. From a72fc4c4d0f6e57f51f20193c18690571e9452d3 Mon Sep 17 00:00:00 2001 From: pckSF Date: Sun, 25 Apr 2021 19:54:08 +0100 Subject: [PATCH 2/3] Resolved comments --- pandas/core/generic.py | 56 ++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 7d630b9c80f53..c8e3ab4e153dd 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6472,30 +6472,40 @@ def fillna( @overload def ffill( self: FrameOrSeries, - axis: None | str | int = ..., + axis: None | Axis = ..., inplace: Literal[False] = ..., limit: None | int = ..., - downcast: None | str | dict = ..., + downcast=..., ) -> FrameOrSeries: ... @overload def ffill( self: FrameOrSeries, - axis: None | str | int = ..., - inplace: Literal[True] = ..., + axis: None | Axis, + inplace: Literal[True], limit: None | int = ..., - downcast: None | str | dict = ..., + downcast=..., ) -> None: ... @overload def ffill( self: FrameOrSeries, - axis: None | str | int = ..., + *, + inplace: Literal[True], + limit: None | int = ..., + downcast=..., + ) -> None: + ... + + @overload + def ffill( + self: FrameOrSeries, + axis: None | Axis = ..., inplace: bool_t = ..., limit: None | int = ..., - downcast: None | str | dict = ..., + downcast=..., ) -> FrameOrSeries | None: ... @@ -6503,10 +6513,10 @@ def ffill( @doc(klass=_shared_doc_kwargs["klass"]) def ffill( self: FrameOrSeries, - axis: None | str | int = None, + axis: None | Axis = None, inplace: bool_t = False, limit: None | int = None, - downcast: None | str | dict = None, + downcast=None, ): """ Synonym for :meth:`DataFrame.fillna` with ``method='ffill'``. @@ -6525,30 +6535,40 @@ def ffill( @overload def bfill( self: FrameOrSeries, - axis: None | str | int = ..., + axis: None | Axis = ..., inplace: Literal[False] = ..., limit: None | int = ..., - downcast: None | str | dict = ..., + downcast=..., ) -> FrameOrSeries: ... @overload def bfill( self: FrameOrSeries, - axis: None | str | int = ..., - inplace: Literal[True] = ..., + axis: None | Axis, + inplace: Literal[True], limit: None | int = ..., - downcast: None | str | dict = ..., + downcast=..., ) -> None: ... @overload def bfill( self: FrameOrSeries, - axis: None | str | int = ..., + *, + inplace: Literal[True], + limit: None | int = ..., + downcast=..., + ) -> None: + ... + + @overload + def bfill( + self: FrameOrSeries, + axis: None | Axis = ..., inplace: bool_t = ..., limit: None | int = ..., - downcast: None | str | dict = ..., + downcast=..., ) -> FrameOrSeries | None: ... @@ -6556,10 +6576,10 @@ def bfill( @doc(klass=_shared_doc_kwargs["klass"]) def bfill( self: FrameOrSeries, - axis: None | str | int = None, + axis: None | Axis = None, inplace: bool_t = False, limit: None | int = None, - downcast: None | str | dict = None, + downcast=None, ): """ Synonym for :meth:`DataFrame.fillna` with ``method='bfill'``. From 860fb3caca892ea8cf26f9f3832a2a035c0bf2e7 Mon Sep 17 00:00:00 2001 From: pckSF Date: Sun, 25 Apr 2021 21:45:49 +0100 Subject: [PATCH 3/3] Keep the return types --- pandas/core/generic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index c8e3ab4e153dd..1e8e161f02302 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6517,7 +6517,7 @@ def ffill( inplace: bool_t = False, limit: None | int = None, downcast=None, - ): + ) -> FrameOrSeries | None: """ Synonym for :meth:`DataFrame.fillna` with ``method='ffill'``. @@ -6580,7 +6580,7 @@ def bfill( inplace: bool_t = False, limit: None | int = None, downcast=None, - ): + ) -> FrameOrSeries | None: """ Synonym for :meth:`DataFrame.fillna` with ``method='bfill'``.