From 6d06bb4c0215da92aea689a6980bcf94234a54dd Mon Sep 17 00:00:00 2001 From: Jan-Eric Nitschke <47750513+JanEricNitschke@users.noreply.github.com> Date: Fri, 28 Jun 2024 17:56:03 +0200 Subject: [PATCH 1/3] Add type parameter to remaining Series.astype overloads. --- pandas-stubs/core/series.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index d6ccca521..28f1d15da 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1320,14 +1320,14 @@ class Series(IndexOpsMixin[S1], NDFrame): dtype: CategoryDtypeArg, copy: _bool = ..., errors: IgnoreRaise = ..., - ) -> Series: ... + ) -> Series[Any]: ... @overload def astype( self, dtype: ObjectDtypeArg | VoidDtypeArg | ExtensionDtype | DtypeObj, copy: _bool = ..., errors: IgnoreRaise = ..., - ) -> Series: ... + ) -> Series[Any]: ... def copy(self, deep: _bool = ...) -> Series[S1]: ... def infer_objects(self) -> Series[S1]: ... @overload From 3b343f7c58c9943b9973ab89406d709a8a3d573c Mon Sep 17 00:00:00 2001 From: Jan-Eric Nitschke <47750513+JanEricNitschke@users.noreply.github.com> Date: Fri, 28 Jun 2024 20:44:13 +0200 Subject: [PATCH 2/3] Make on overload more precise. --- pandas-stubs/core/series.pyi | 2 +- tests/test_series.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 28f1d15da..1b894dd11 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1320,7 +1320,7 @@ class Series(IndexOpsMixin[S1], NDFrame): dtype: CategoryDtypeArg, copy: _bool = ..., errors: IgnoreRaise = ..., - ) -> Series[Any]: ... + ) -> Series[CategoryDtypeArg]: ... @overload def astype( self, diff --git a/tests/test_series.py b/tests/test_series.py index 35e9ee063..263aeed06 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -2581,8 +2581,8 @@ def test_astype_categorical(cast_arg: CategoryDtypeArg, target_type: type) -> No if TYPE_CHECKING: # pandas category - assert_type(s.astype(pd.CategoricalDtype()), "pd.Series[Any]") - assert_type(s.astype("category"), "pd.Series[Any]") + assert_type(s.astype(pd.CategoricalDtype()), "pd.Series[CategoryDtypeArg]") + assert_type(s.astype("category"), "pd.Series[CategoryDtypeArg]") # pyarrow dictionary # assert_type(s.astype("dictionary[pyarrow]"), "pd.Series[Categorical]") From 95efb95802f57130f9985566849b6d3961e2edfe Mon Sep 17 00:00:00 2001 From: Jan-Eric Nitschke <47750513+JanEricNitschke@users.noreply.github.com> Date: Fri, 28 Jun 2024 21:07:47 +0200 Subject: [PATCH 3/3] Fix CategoricalDtype --- pandas-stubs/core/series.pyi | 3 ++- tests/test_series.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 1b894dd11..2cb50fc38 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -158,6 +158,7 @@ from pandas._typing import ( ) from pandas.core.dtypes.base import ExtensionDtype +from pandas.core.dtypes.dtypes import CategoricalDtype from pandas.plotting import PlotAccessor @@ -1320,7 +1321,7 @@ class Series(IndexOpsMixin[S1], NDFrame): dtype: CategoryDtypeArg, copy: _bool = ..., errors: IgnoreRaise = ..., - ) -> Series[CategoryDtypeArg]: ... + ) -> Series[CategoricalDtype]: ... @overload def astype( self, diff --git a/tests/test_series.py b/tests/test_series.py index 263aeed06..8f948faba 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -2581,8 +2581,8 @@ def test_astype_categorical(cast_arg: CategoryDtypeArg, target_type: type) -> No if TYPE_CHECKING: # pandas category - assert_type(s.astype(pd.CategoricalDtype()), "pd.Series[CategoryDtypeArg]") - assert_type(s.astype("category"), "pd.Series[CategoryDtypeArg]") + assert_type(s.astype(pd.CategoricalDtype()), "pd.Series[pd.CategoricalDtype]") + assert_type(s.astype("category"), "pd.Series[pd.CategoricalDtype]") # pyarrow dictionary # assert_type(s.astype("dictionary[pyarrow]"), "pd.Series[Categorical]")