From 21d8efa183a116af55dd05fe60993108aeb829d6 Mon Sep 17 00:00:00 2001 From: BeanNan Date: Sat, 9 Jan 2021 22:08:30 +0800 Subject: [PATCH 1/4] TYP: series apply method adds type hints --- pandas/core/series.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 668cad4f64ac3..6147c3f30db42 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -3979,7 +3979,9 @@ def transform( ) -> FrameOrSeriesUnion: return transform(self, func, axis, *args, **kwargs) - def apply(self, func, convert_dtype=True, args=(), **kwds): + def apply( + self, func: AggFuncType, convert_dtype: bool = True, args: tuple = (), **kwds + ): """ Invoke function on values of Series. From b3e27c78b925b0f01869d4f1391b1aa6b95ada1c Mon Sep 17 00:00:00 2001 From: BeanNan Date: Sat, 9 Jan 2021 22:57:21 +0800 Subject: [PATCH 2/4] TYP: Additional type hints --- pandas/core/series.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 6147c3f30db42..0763512ac64e9 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -3980,8 +3980,12 @@ def transform( return transform(self, func, axis, *args, **kwargs) def apply( - self, func: AggFuncType, convert_dtype: bool = True, args: tuple = (), **kwds - ): + self, + func: AggFuncType, + convert_dtype: bool = True, + args: Tuple[Any] = (), + **kwds, + ) -> FrameOrSeriesUnion: """ Invoke function on values of Series. From 5d71e81f41f5a1084f2501605b9c6ff48488ff45 Mon Sep 17 00:00:00 2001 From: BeanNan Date: Sat, 9 Jan 2021 23:31:30 +0800 Subject: [PATCH 3/4] TYP: fix mypy error --- pandas/core/series.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 0763512ac64e9..cf47e1daae311 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -15,6 +15,7 @@ Tuple, Type, Union, + cast, ) import warnings @@ -3983,7 +3984,7 @@ def apply( self, func: AggFuncType, convert_dtype: bool = True, - args: Tuple[Any] = (), + args: Tuple[Any, ...] = (), **kwds, ) -> FrameOrSeriesUnion: """ @@ -4099,7 +4100,8 @@ def apply( if kwds or args and not isinstance(func, np.ufunc): def f(x): - return func(x, *args, **kwds) + _func = cast(Callable[..., Any], func) + return _func(x, *args, **kwds) else: f = func From 97c0274df178f5bd7c8a852bd812dc2e476ec0b2 Mon Sep 17 00:00:00 2001 From: BeanNan Date: Sun, 10 Jan 2021 00:02:55 +0800 Subject: [PATCH 4/4] TYP: fix sas7bdat type hints --- pandas/io/sas/sas7bdat.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/io/sas/sas7bdat.py b/pandas/io/sas/sas7bdat.py index 7c2b801ee0ea8..fe06f103e6c5e 100644 --- a/pandas/io/sas/sas7bdat.py +++ b/pandas/io/sas/sas7bdat.py @@ -52,13 +52,17 @@ def _convert_datetimes(sas_datetimes: pd.Series, unit: str) -> pd.Series: return pd.to_datetime(sas_datetimes, unit=unit, origin="1960-01-01") except OutOfBoundsDatetime: if unit == "s": - return sas_datetimes.apply( + s_series = sas_datetimes.apply( lambda sas_float: datetime(1960, 1, 1) + timedelta(seconds=sas_float) ) + s_series = cast(pd.Series, s_series) + return s_series elif unit == "d": - return sas_datetimes.apply( + d_series = sas_datetimes.apply( lambda sas_float: datetime(1960, 1, 1) + timedelta(days=sas_float) ) + d_series = cast(pd.Series, d_series) + return d_series else: raise ValueError("unit must be 'd' or 's'")