From 0469f4702f0b24f162fcb54ff4704240ef26d2fa Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Thu, 26 Jan 2023 02:29:03 +0530 Subject: [PATCH 01/32] adding overloads to astype --- pandas-stubs/core/series.pyi | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index c9075dcb5..0cf1898fa 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1,3 +1,9 @@ +from core.api import( + Int8Dtype as Int8Dtype, + Int16Dtype as Int16Dtype, + Int32Dtype as Int32Dtype, + Int64Dtype as Int64Dtype, +) from collections.abc import ( Callable, Hashable, @@ -1025,12 +1031,26 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): axis: SeriesAxisType | None = ..., ignore_index: _bool = ..., ) -> Series[S1]: ... + #def astype( + # self, + # dtype: S1 | _str | type[Scalar], + # copy: _bool = ..., + #errors: IgnoreRaise = ..., + #) -> Series: ... + @overload def astype( self, - dtype: S1 | _str | type[Scalar], + dtype: S1, _str, copy: _bool = ..., - errors: IgnoreRaise = ..., - ) -> Series: ... + errors: IgnoreRaise = ... + ) -> Series[str]: ... + @overload + def astype( + self, + dtype: S1, Int8Dtype, Int16Dtype, Int32Dtype, Int64Dtype, + copy: _bool = ..., + errors: IgnoreRaise = ... + ) -> Series[int]: ... def copy(self, deep: _bool = ...) -> Series[S1]: ... def infer_objects(self) -> Series[S1]: ... def convert_dtypes( From f0317dec1918a826d927e0e235a2dd1606cd079d Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Thu, 26 Jan 2023 15:29:26 +0530 Subject: [PATCH 02/32] Created the table for astype --- table.rst | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 table.rst diff --git a/table.rst b/table.rst new file mode 100644 index 000000000..f9b31c647 --- /dev/null +++ b/table.rst @@ -0,0 +1,43 @@ + +Table for astype + + ++-------------------+-------------------------+ +| S1 | Dtype | ++===================+=========================+ +| int | Int8Dtype,Int16Dtype, | +| | Int32Dtype,Int54Dtype, | ++-------------------+-------------------------+ +| str | StringDtype,str | ++-------------------+-------------------------+ +| bytes | BolleanDtype | ++-------------------+-------------------------+ +| float |Float32Dtype,Float64Dtype| ++-------------------+-------------------------+ +| complex | | ++-------------------+-------------------------+ +| Timestamp | Timestamp | ++-------------------+-------------------------+ +| Timedelta | Timedelta,TimedeltaIndex| ++-------------------+-------------------------+ +| Period | Period, PeriodDtype | +| | PeriodIndex | ++-------------------+-------------------------+ +| np.timedelta64 | | ++-------------------+-------------------------+ +| | np.datetime64 | ++-------------------+-------------------------+ +| | np.int | ++-------------------+-------------------------+ +| | np.float | ++-------------------+-------------------------+ +| "S1" | "int","float" | ++-------------------+-------------------------+ +| | | +| | | +| | | +| | | +| | | +| | | +| | | ++-------------------+-------------------------+ \ No newline at end of file From 3a75501a1207cd711f4f0292b0171ae17a67cc24 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Thu, 26 Jan 2023 21:31:01 +0530 Subject: [PATCH 03/32] Update table.rst --- table.rst | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/table.rst b/table.rst index f9b31c647..5126a0c5b 100644 --- a/table.rst +++ b/table.rst @@ -5,39 +5,36 @@ Table for astype +-------------------+-------------------------+ | S1 | Dtype | +===================+=========================+ -| int | Int8Dtype,Int16Dtype, | -| | Int32Dtype,Int54Dtype, | +| int | pd.Int8Dtype, | +| | pd.Int16Dtype, | +| | pd.Int32Dtype, | +| | pd.Int54Dtype, | +| | "int",int,np.int8, | +| | np.int16,np.int32, | +| | np.int64,np.uint8 | +| | np.uint16,np.uint32 | +| | np.uint64,np.intp | +| | np.uintp, | +-------------------+-------------------------+ | str | StringDtype,str | +-------------------+-------------------------+ -| bytes | BolleanDtype | +| bytes | np.byte,np.ubyte,bytes | +-------------------+-------------------------+ -| float |Float32Dtype,Float64Dtype| +| float | pd.Float32Dtype, | +| | pd.Float64Dtype, | +| | float,"float",np.float16| +| | np.float32,np.float64 | +| | np.float96,np.float128 | +-------------------+-------------------------+ -| complex | | +| complex | "complex",np.complex64, | +| | np.complex128, | +| | np.complex192, | +| | np.complex256 | +-------------------+-------------------------+ -| Timestamp | Timestamp | +| bool | pd.BooleanDtype, bool | +| | np.bool_ | +-------------------+-------------------------+ -| Timedelta | Timedelta,TimedeltaIndex| +| | np.timedelta64 | +-------------------+-------------------------+ -| Period | Period, PeriodDtype | -| | PeriodIndex | -+-------------------+-------------------------+ -| np.timedelta64 | | -+-------------------+-------------------------+ -| | np.datetime64 | -+-------------------+-------------------------+ -| | np.int | -+-------------------+-------------------------+ -| | np.float | -+-------------------+-------------------------+ -| "S1" | "int","float" | -+-------------------+-------------------------+ -| | | -| | | -| | | -| | | -| | | -| | | -| | | +| np.datetime64 | | +-------------------+-------------------------+ \ No newline at end of file From c5a20737a1a94de49bce730d6fca01b75b716c6f Mon Sep 17 00:00:00 2001 From: ram vikram singh Date: Thu, 26 Jan 2023 21:36:23 +0530 Subject: [PATCH 04/32] Updated the table and added numpy dtypes --- table.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/table.rst b/table.rst index 5126a0c5b..f51066962 100644 --- a/table.rst +++ b/table.rst @@ -32,9 +32,9 @@ Table for astype | | np.complex256 | +-------------------+-------------------------+ | bool | pd.BooleanDtype, bool | -| | np.bool_ | +| | np.bool_, | +-------------------+-------------------------+ | | np.timedelta64 | +-------------------+-------------------------+ | np.datetime64 | | -+-------------------+-------------------------+ \ No newline at end of file ++-------------------+-------------------------+ From 67fa6da66e666f5a341457ad5c9d1c5f6e4535ef Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Thu, 26 Jan 2023 22:41:46 +0530 Subject: [PATCH 05/32] Update table.rst --- table.rst | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/table.rst b/table.rst index f51066962..99ef05c4a 100644 --- a/table.rst +++ b/table.rst @@ -14,27 +14,32 @@ Table for astype | | np.int64,np.uint8 | | | np.uint16,np.uint32 | | | np.uint64,np.intp | -| | np.uintp, | +| | np.uintp,np.integer | +-------------------+-------------------------+ -| str | StringDtype,str | +| str | StringDtype,str,"str" | +-------------------+-------------------------+ | bytes | np.byte,np.ubyte,bytes | +| | "bytes" | +-------------------+-------------------------+ | float | pd.Float32Dtype, | | | pd.Float64Dtype, | | | float,"float",np.float16| | | np.float32,np.float64 | | | np.float96,np.float128 | +| | np.floating | +-------------------+-------------------------+ | complex | "complex",np.complex64, | | | np.complex128, | | | np.complex192, | -| | np.complex256 | +| | np.complex256, | +| | complex | +-------------------+-------------------------+ | bool | pd.BooleanDtype, bool | -| | np.bool_, | +| | np.bool_ | +| | "bool" | +-------------------+-------------------------+ -| | np.timedelta64 | +| np.timedelta64 | np.timedelta64 | +-------------------+-------------------------+ -| np.datetime64 | | +| np.datetime64 | np.datetime64 | +-------------------+-------------------------+ + From 0cc665b9a4bf454477343695fbbe4b613899a041 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Tue, 7 Feb 2023 21:20:55 +0530 Subject: [PATCH 06/32] updated np.datetime64 --- table.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/table.rst b/table.rst index 99ef05c4a..dd253dbd4 100644 --- a/table.rst +++ b/table.rst @@ -41,5 +41,6 @@ Table for astype | np.timedelta64 | np.timedelta64 | +-------------------+-------------------------+ | np.datetime64 | np.datetime64 | +| | "datetime64[ns]" | +-------------------+-------------------------+ From 96903fb791e4c13e1af3cdd5a1c195d129c7b4f8 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Tue, 7 Feb 2023 22:20:34 +0530 Subject: [PATCH 07/32] Update table.rst --- table.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/table.rst b/table.rst index dd253dbd4..408d56208 100644 --- a/table.rst +++ b/table.rst @@ -38,9 +38,12 @@ Table for astype | | np.bool_ | | | "bool" | +-------------------+-------------------------+ -| np.timedelta64 | np.timedelta64 | +| Timedelta | np.timedelta64, | +| | Timedelta | +| | | +-------------------+-------------------------+ -| np.datetime64 | np.datetime64 | +| Timestamp | Timestamp | | | "datetime64[ns]" | +| | | +-------------------+-------------------------+ From e61394d650f83c12b07681a21e1d99becf6e288c Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Tue, 7 Feb 2023 22:21:41 +0530 Subject: [PATCH 08/32] added types in Timedelta --- table.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/table.rst b/table.rst index 408d56208..1b3338d03 100644 --- a/table.rst +++ b/table.rst @@ -39,11 +39,11 @@ Table for astype | | "bool" | +-------------------+-------------------------+ | Timedelta | np.timedelta64, | -| | Timedelta | +| | Timedelta, | +| | "timedelta64[ns]" | | | | +-------------------+-------------------------+ | Timestamp | Timestamp | | | "datetime64[ns]" | -| | | +-------------------+-------------------------+ From 4fa513aceac5929cc841fd801b02ef87774513bd Mon Sep 17 00:00:00 2001 From: ram vikram singh Date: Tue, 7 Feb 2023 23:50:04 +0530 Subject: [PATCH 09/32] removed not required args in Dtype --- table.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/table.rst b/table.rst index 1b3338d03..a829cc38f 100644 --- a/table.rst +++ b/table.rst @@ -39,11 +39,9 @@ Table for astype | | "bool" | +-------------------+-------------------------+ | Timedelta | np.timedelta64, | -| | Timedelta, | | | "timedelta64[ns]" | | | | +-------------------+-------------------------+ -| Timestamp | Timestamp | -| | "datetime64[ns]" | +| Timestamp | "datetime64[ns]" | +-------------------+-------------------------+ From 848753ee2bf4ca2fda2394b969cbbdd102fb8c96 Mon Sep 17 00:00:00 2001 From: ram vikram singh Date: Wed, 8 Feb 2023 00:40:12 +0530 Subject: [PATCH 10/32] removed np.timedelta64 in Timedelta --- table.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/table.rst b/table.rst index a829cc38f..4843ef0bd 100644 --- a/table.rst +++ b/table.rst @@ -38,8 +38,7 @@ Table for astype | | np.bool_ | | | "bool" | +-------------------+-------------------------+ -| Timedelta | np.timedelta64, | -| | "timedelta64[ns]" | +| Timedelta | "timedelta64[ns]" | | | | +-------------------+-------------------------+ | Timestamp | "datetime64[ns]" | From d8f8616a2d53a52e1e787b2667ed6c360f650858 Mon Sep 17 00:00:00 2001 From: ram vikram singh Date: Wed, 8 Feb 2023 01:16:57 +0530 Subject: [PATCH 11/32] Removed timedelta64 --- table.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/table.rst b/table.rst index 4843ef0bd..f049c7926 100644 --- a/table.rst +++ b/table.rst @@ -38,7 +38,7 @@ Table for astype | | np.bool_ | | | "bool" | +-------------------+-------------------------+ -| Timedelta | "timedelta64[ns]" | +| Timedelta | | | | | +-------------------+-------------------------+ | Timestamp | "datetime64[ns]" | From 814df473ff45aaae58c15504dad70c44cf09e97b Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Fri, 17 Feb 2023 00:51:13 +0530 Subject: [PATCH 12/32] expanding series astype --- pandas-stubs/core/series.pyi | 107 +++++++++++++++++++++++++++++------ 1 file changed, 89 insertions(+), 18 deletions(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 0cf1898fa..031d09476 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1,9 +1,3 @@ -from core.api import( - Int8Dtype as Int8Dtype, - Int16Dtype as Int16Dtype, - Int32Dtype as Int32Dtype, - Int64Dtype as Int64Dtype, -) from collections.abc import ( Callable, Hashable, @@ -27,11 +21,18 @@ from typing import ( overload, ) +from core.api import ( + Int8Dtype as Int8Dtype, + Int16Dtype as Int16Dtype, + Int32Dtype as Int32Dtype, + Int64Dtype as Int64Dtype, +) from matplotlib.axes import ( Axes as PlotAxes, SubplotBase, ) import numpy as np +import pandas as pd from pandas import ( Period, Timedelta, @@ -1031,26 +1032,96 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): axis: SeriesAxisType | None = ..., ignore_index: _bool = ..., ) -> Series[S1]: ... - #def astype( - # self, - # dtype: S1 | _str | type[Scalar], - # copy: _bool = ..., - #errors: IgnoreRaise = ..., - #) -> Series: ... @overload def astype( self, - dtype: S1, _str, + dtype: S1 + | pd.Int8Dtype + | pd.Int16Dtype + | pd.Int32Dtype + | pd.Int64Dtype + | np.int8 + | np.int16 + | np.int32 + | np.int64 + | np.uint8 + | np.uint16 + | np.uint32 + | np.uint64 + | np.intp + | np.uintp + | np.integer + | int + | Literal["int"], copy: _bool = ..., - errors: IgnoreRaise = ... - ) -> Series[str]: ... + errors: IgnoreRaise = ..., + ) -> Series[int]: ... @overload def astype( self, - dtype: S1, Int8Dtype, Int16Dtype, Int32Dtype, Int64Dtype, + dtype: S1 | _str | pd.StringDtype | Literal["str"], copy: _bool = ..., - errors: IgnoreRaise = ... - ) -> Series[int]: ... + errors: IgnoreRaise = ..., + ) -> Series[str]: ... + @overload + def astype( + self, + dtype: S1 | np.byte | np.ubyte | bytes, + copy: _bool = ..., + errors: IgnoreRaise = ..., + ) -> Series[bytes]: ... + @overload + def astype( + self, + dtype: S1 + | pd.Float32Dtype + | pd.Float64Dtype + | np.float16 + | np.float32 + | np.float64 + | np.float96 + | np.float128 + | np.floating + | float + | Literal["float"], + copy: _bool = ..., + errors: IgnoreRaise = ..., + ) -> Series[float]: ... + @overload + def astype( + self, + dtype: S1 + | np.complex64 + | np.complex128 + | np.complex192 + | np.complex256 + | complex + | Literal["complex"], + copy: _bool = ..., + errors: IgnoreRaise = ..., + # ) -> Series: ... + ) -> Series[complex]: ... + @overload + def astype( + self, + dtype: S1 | pd.BooleanDtype | np.bool | bool | Literal["bool"], + copy: _bool = ..., + errors: IgnoreRaise = ..., + ) -> Series[bool]: ... + @overload + def astype( + self, + dtype: S1, + copy: _bool = ..., + errors: IgnoreRaise = ..., + ) -> Series[Timedelta]: ... + @overload + def astype( + self, + dtype: S1 | Literal["datetime64[ns]"], + copy: _bool = ..., + errors: IgnoreRaise = ..., + ) -> Series[Timestamp]: ... def copy(self, deep: _bool = ...) -> Series[S1]: ... def infer_objects(self) -> Series[S1]: ... def convert_dtypes( From dea00425b6cf08d882451fcf4e68792986fa1e10 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Fri, 17 Feb 2023 23:28:07 +0530 Subject: [PATCH 13/32] Added type in args in dtype --- pandas-stubs/core/generic.pyi | 6 --- pandas-stubs/core/series.pyi | 88 +++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 41 deletions(-) diff --git a/pandas-stubs/core/generic.pyi b/pandas-stubs/core/generic.pyi index fa7589270..557607d9f 100644 --- a/pandas-stubs/core/generic.pyi +++ b/pandas-stubs/core/generic.pyi @@ -374,12 +374,6 @@ class NDFrame(PandasObject, indexing.IndexingMixin): def values(self) -> ArrayLike: ... @property def dtypes(self): ... - def astype( - self: NDFrameT, - dtype, - copy: _bool = ..., - errors: IgnoreRaise = ..., - ) -> NDFrameT: ... def copy(self: NDFrameT, deep: _bool = ...) -> NDFrameT: ... def __copy__(self, deep: _bool = ...) -> NDFrame: ... def __deepcopy__(self, memo=...) -> NDFrame: ... diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 031d09476..23478763d 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1033,56 +1033,76 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): ignore_index: _bool = ..., ) -> Series[S1]: ... @overload + # def astype( + # self, + # dtype: Union[ + # Literal["int"], + # int, + # pd.Int8Dtype, + # pd.Int16Dtype, + # pd.Int16Dtype, + # pd.Int32Dtype, + # pd.Int64Dtype, + # np.int8, + # np.int16, + # np.int32, + # np.int64, + # np.uint8, + # np.uint16, + # np.uint32, + # np.uint64, + # np.intp, + # np.uintp, + # np.integer, + # ], def astype( self, - dtype: S1 + dtype: Literal["int"] + | type[int] | pd.Int8Dtype | pd.Int16Dtype | pd.Int32Dtype | pd.Int64Dtype - | np.int8 - | np.int16 - | np.int32 - | np.int64 - | np.uint8 - | np.uint16 - | np.uint32 - | np.uint64 - | np.intp - | np.uintp - | np.integer - | int - | Literal["int"], + | type[np.int8] + | type[np.int16] + | type[np.int32] + | type[np.int64] + | type[np.uint8] + | type[np.uint16] + | type[np.uint32] + | type[np.uint64] + | type[np.intp] + | type[np.uintp] + | type[np.integer], copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[int]: ... @overload def astype( self, - dtype: S1 | _str | pd.StringDtype | Literal["str"], + dtype: type[_str] | pd.StringDtype | Literal["str"], copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[str]: ... @overload def astype( self, - dtype: S1 | np.byte | np.ubyte | bytes, + dtype: type[np.byte] | type[np.ubyte] | bytes, copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[bytes]: ... @overload def astype( self, - dtype: S1 - | pd.Float32Dtype + dtype: pd.Float32Dtype | pd.Float64Dtype - | np.float16 - | np.float32 - | np.float64 - | np.float96 - | np.float128 - | np.floating - | float + | type[np.float16] + | type[np.float32] + | type[np.float64] + | type[np.float96] + | type[np.float128] + | type[np.floating] + | type[float] | Literal["float"], copy: _bool = ..., errors: IgnoreRaise = ..., @@ -1090,35 +1110,33 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): @overload def astype( self, - dtype: S1 - | np.complex64 - | np.complex128 - | np.complex192 - | np.complex256 - | complex + dtype: type[np.complex64] + | type[np.complex128] + | type[np.complex192] + | type[np.complex256] + | type[complex] | Literal["complex"], copy: _bool = ..., errors: IgnoreRaise = ..., - # ) -> Series: ... ) -> Series[complex]: ... @overload def astype( self, - dtype: S1 | pd.BooleanDtype | np.bool | bool | Literal["bool"], + dtype: pd.BooleanDtype | type[np.bool_] | type[bool] | Literal["bool"], copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[bool]: ... @overload def astype( self, - dtype: S1, + dtype: Literal["timedelta64[ns]"], copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[Timedelta]: ... @overload def astype( self, - dtype: S1 | Literal["datetime64[ns]"], + dtype: Literal["datetime64[ns]"], copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[Timestamp]: ... From 0e84068a5ddf1e39268c2d7c4797f8f3025efa29 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Sat, 18 Feb 2023 00:40:57 +0530 Subject: [PATCH 14/32] corrected the args --- pandas-stubs/core/series.pyi | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 23478763d..55134bccb 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1073,21 +1073,22 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): | type[np.uint64] | type[np.intp] | type[np.uintp] - | type[np.integer], + | type[np.integer] + |type[np.byte] | type[np.ubyte], copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[int]: ... @overload def astype( self, - dtype: type[_str] | pd.StringDtype | Literal["str"], + dtype: type[str] | pd.StringDtype | Literal["str"], copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[str]: ... @overload def astype( self, - dtype: type[np.byte] | type[np.ubyte] | bytes, + dtype: bytes, copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[bytes]: ... @@ -1122,7 +1123,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): @overload def astype( self, - dtype: pd.BooleanDtype | type[np.bool_] | type[bool] | Literal["bool"], + dtype: pd.BooleanDtype | type[bool] | Literal["bool"], copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[bool]: ... From a8654847d6ffeb4fbb28774b7e30e3b310cba4a0 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Mon, 20 Feb 2023 01:03:44 +0530 Subject: [PATCH 15/32] adding a overload for 'category' and normal changes --- pandas-stubs/core/series.pyi | 41 ++++++++++++------------------------ 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 55134bccb..675699fc1 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1033,32 +1033,11 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): ignore_index: _bool = ..., ) -> Series[S1]: ... @overload - # def astype( - # self, - # dtype: Union[ - # Literal["int"], - # int, - # pd.Int8Dtype, - # pd.Int16Dtype, - # pd.Int16Dtype, - # pd.Int32Dtype, - # pd.Int64Dtype, - # np.int8, - # np.int16, - # np.int32, - # np.int64, - # np.uint8, - # np.uint16, - # np.uint32, - # np.uint64, - # np.intp, - # np.uintp, - # np.integer, - # ], def astype( self, - dtype: Literal["int"] + dtype: Literal["int", "int32"] | type[int] + | type[bool] | pd.Int8Dtype | pd.Int16Dtype | pd.Int32Dtype @@ -1074,17 +1053,18 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): | type[np.intp] | type[np.uintp] | type[np.integer] - |type[np.byte] | type[np.ubyte], + | type[np.byte] + | type[np.ubyte], copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[int]: ... @overload def astype( self, - dtype: type[str] | pd.StringDtype | Literal["str"], + dtype: type[_str] | pd.StringDtype | Literal["str"], copy: _bool = ..., errors: IgnoreRaise = ..., - ) -> Series[str]: ... + ) -> Series[_str]: ... @overload def astype( self, @@ -1123,7 +1103,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): @overload def astype( self, - dtype: pd.BooleanDtype | type[bool] | Literal["bool"], + dtype: pd.BooleanDtype | Literal["bool"], copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[bool]: ... @@ -1141,6 +1121,13 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[Timestamp]: ... + @overload + def astype( + self, + dtype: Literal["category"], + copy: _bool = ..., + errors: IgnoreRaise = ..., + ) -> Series: ... def copy(self, deep: _bool = ...) -> Series[S1]: ... def infer_objects(self) -> Series[S1]: ... def convert_dtypes( From 17ce1aa59d607943c27b1559405c1ffc979e679f Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Mon, 20 Feb 2023 23:52:26 +0530 Subject: [PATCH 16/32] added tests --- pandas-stubs/core/series.pyi | 9 ++--- tests/test_series.py | 70 ++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 675699fc1..2ec5080c4 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1035,9 +1035,8 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): @overload def astype( self, - dtype: Literal["int", "int32"] - | type[int] - | type[bool] + dtype: Literal["int", "int32"] | type[int] | type[bool] + # | type[bytes] | pd.Int8Dtype | pd.Int16Dtype | pd.Int32Dtype @@ -1068,7 +1067,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): @overload def astype( self, - dtype: bytes, + dtype: type[bytes], copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[bytes]: ... @@ -1114,6 +1113,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[Timedelta]: ... + # ) -> TimedeltaSeries: ... @overload def astype( self, @@ -1121,6 +1121,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[Timestamp]: ... + # ) -> TimestampSeries: ... @overload def astype( self, diff --git a/tests/test_series.py b/tests/test_series.py index e2f5c83d7..97305ae14 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -23,11 +23,14 @@ ExtensionArray, ExtensionDtype, ) +from pandas.core.series import Series # noqa: F401 from pandas.core.window import ExponentialMovingWindow import pytest from typing_extensions import assert_type import xarray as xr +from pandas._libs.tslibs.timedeltas import Timedelta +from pandas._libs.tslibs.timestamps import Timestamp from pandas._typing import ( DtypeObj, Scalar, @@ -1384,3 +1387,70 @@ def test_change_to_dict_return_type() -> None: df = pd.DataFrame(zip(id, value), columns=["id", "value"]) fd = df.set_index("id")["value"].to_dict() check(assert_type(fd, Dict[Any, Any]), dict) + + +def test_updated_astype() -> None: + s = pd.Series([3, 4, 5]) + s1 = pd.Series(True) + + check(assert_type(s.astype(int), "Series[int]"), pd.Series, int) + check(assert_type(s.astype("int"), "Series[int]"), pd.Series, int) + check(assert_type(s.astype("int32"), "Series[int]"), pd.Series, int) + check(assert_type(s.astype(bool), "Series[int]"), pd.Series, int) + check(assert_type(s.astype(pd.Int8Dtype()), "Series[int]"), pd.Series) + check(assert_type(s.astype(pd.Int16Dtype()), "Series[int]"), pd.Series) + check(assert_type(s.astype(pd.Int32Dtype()), "Series[int]"), pd.Series) + check(assert_type(s.astype(pd.Int64Dtype()), "Series[int]"), pd.Series) + check(assert_type(s.astype(np.int8), "Series[int]"), pd.Series, int) + check(assert_type(s.astype(np.int16), "Series[int]"), pd.Series, int) + check(assert_type(s.astype(np.int32), "Series[int]"), pd.Series, int) + check(assert_type(s.astype(np.int64), "Series[int]"), pd.Series, int) + check(assert_type(s.astype(np.uint8), "Series[int]"), pd.Series, int) + check(assert_type(s.astype(np.uint16), "Series[int]"), pd.Series, int) + check(assert_type(s.astype(np.uint32), "Series[int]"), pd.Series, int) + check(assert_type(s.astype(np.uint64), "Series[int]"), pd.Series, int) + check(assert_type(s.astype(np.intp), "Series[int]"), pd.Series, int) + check(assert_type(s.astype(np.uintp), "Series[int]"), pd.Series, int) + # check(assert_type(s.astype(np.integer), "Series[int]"), pd.Series) + check(assert_type(s.astype(np.byte), "Series[int]"), pd.Series, int) + check(assert_type(s.astype(np.ubyte), "Series[int]"), pd.Series, int) + + check(assert_type(s.astype(str), "Series[str]"), pd.Series, str) + # check(assert_type(s.astype(pd.StringDtype()), "Series[str]"), pd.Series) + check(assert_type(s.astype("str"), "Series[str]"), pd.Series, str) + + check(assert_type(s.astype(bytes), "Series[bytes]"), pd.Series) + + check(assert_type(s.astype(pd.Float32Dtype()), "Series[float]"), pd.Series) + check(assert_type(s.astype(pd.Float64Dtype()), "Series[float]"), pd.Series) + check(assert_type(s.astype(np.float16), "Series[float]"), pd.Series, float) + check(assert_type(s.astype(np.float32), "Series[float]"), pd.Series, float) + check(assert_type(s.astype(np.float64), "Series[float]"), pd.Series, float) + # check(assert_type(s.astype(np.float96), "Series[float]"), pd.Series) + # check(assert_type(s.astype(np.float96), "Series[float]"), pd.Series, float) + # check(assert_type(s.astype(np.float128), "Series[float]"), pd.Series) + # check(assert_type(s.astype(np.floating), "Series[float]"), pd.Series, float) + check(assert_type(s.astype(float), "Series[float]"), pd.Series, float) + check(assert_type(s.astype("float"), "Series[float]"), pd.Series, float) + + check(assert_type(s.astype(np.complex64), "Series[complex]"), pd.Series, complex) + check(assert_type(s.astype(np.complex128), "Series[complex]"), pd.Series, complex) + # check(assert_type(s.astype(np.complex196), "Series[complex]"), pd.Series, complex) + # check(assert_type(s.astype(np.complex256), "Series[complex]"), pd.Series) + check(assert_type(s.astype(complex), "Series[complex]"), pd.Series, complex) + check(assert_type(s.astype("complex"), "Series[complex]"), pd.Series, complex) + + check(assert_type(s1.astype(pd.BooleanDtype()), "Series[bool]"), pd.Series) + check(assert_type(s.astype("bool"), "Series[bool]"), pd.Series, bool) + + check( + assert_type(s.astype("timedelta64[ns]"), "Series[Timedelta]"), + pd.Series, + Timedelta, + ) + + check( + assert_type(s.astype("datetime64[ns]"), "Series[Timestamp]"), + pd.Series, + Timestamp, + ) From 7a84f4019362dbb1ec426ae93b510bd5dd794cb1 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Tue, 21 Feb 2023 00:58:03 +0530 Subject: [PATCH 17/32] removed unused args --- pandas-stubs/core/series.pyi | 4 ---- tests/test_series.py | 11 +++-------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 2ec5080c4..6773b48f7 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1079,8 +1079,6 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): | type[np.float16] | type[np.float32] | type[np.float64] - | type[np.float96] - | type[np.float128] | type[np.floating] | type[float] | Literal["float"], @@ -1092,8 +1090,6 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): self, dtype: type[np.complex64] | type[np.complex128] - | type[np.complex192] - | type[np.complex256] | type[complex] | Literal["complex"], copy: _bool = ..., diff --git a/tests/test_series.py b/tests/test_series.py index 97305ae14..3c553f07b 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -1411,12 +1411,12 @@ def test_updated_astype() -> None: check(assert_type(s.astype(np.uint64), "Series[int]"), pd.Series, int) check(assert_type(s.astype(np.intp), "Series[int]"), pd.Series, int) check(assert_type(s.astype(np.uintp), "Series[int]"), pd.Series, int) - # check(assert_type(s.astype(np.integer), "Series[int]"), pd.Series) + check(assert_type(s.astype(np.integer), "Series[int]"), pd.Series) check(assert_type(s.astype(np.byte), "Series[int]"), pd.Series, int) check(assert_type(s.astype(np.ubyte), "Series[int]"), pd.Series, int) check(assert_type(s.astype(str), "Series[str]"), pd.Series, str) - # check(assert_type(s.astype(pd.StringDtype()), "Series[str]"), pd.Series) + check(assert_type(s.astype(pd.StringDtype()), "Series[str]"), pd.Series) check(assert_type(s.astype("str"), "Series[str]"), pd.Series, str) check(assert_type(s.astype(bytes), "Series[bytes]"), pd.Series) @@ -1426,17 +1426,12 @@ def test_updated_astype() -> None: check(assert_type(s.astype(np.float16), "Series[float]"), pd.Series, float) check(assert_type(s.astype(np.float32), "Series[float]"), pd.Series, float) check(assert_type(s.astype(np.float64), "Series[float]"), pd.Series, float) - # check(assert_type(s.astype(np.float96), "Series[float]"), pd.Series) - # check(assert_type(s.astype(np.float96), "Series[float]"), pd.Series, float) - # check(assert_type(s.astype(np.float128), "Series[float]"), pd.Series) - # check(assert_type(s.astype(np.floating), "Series[float]"), pd.Series, float) + check(assert_type(s.astype(np.floating), "Series[float]"), pd.Series) check(assert_type(s.astype(float), "Series[float]"), pd.Series, float) check(assert_type(s.astype("float"), "Series[float]"), pd.Series, float) check(assert_type(s.astype(np.complex64), "Series[complex]"), pd.Series, complex) check(assert_type(s.astype(np.complex128), "Series[complex]"), pd.Series, complex) - # check(assert_type(s.astype(np.complex196), "Series[complex]"), pd.Series, complex) - # check(assert_type(s.astype(np.complex256), "Series[complex]"), pd.Series) check(assert_type(s.astype(complex), "Series[complex]"), pd.Series, complex) check(assert_type(s.astype("complex"), "Series[complex]"), pd.Series, complex) From 7fb2c3f7e0262f02fa36adc825f4fa7d8efda1ec Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Thu, 23 Feb 2023 22:29:15 +0530 Subject: [PATCH 18/32] corrected tests --- pandas-stubs/core/series.pyi | 9 ++---- tests/test_series.py | 54 +++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 0ee51a8bc..2bcb18cd5 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1045,8 +1045,9 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): @overload def astype( self, - dtype: Literal["int", "int32"] | type[int] | type[bool] - # | type[bytes] + dtype: Literal["int", "int32"] + | type[int] + | type[bool] | pd.Int8Dtype | pd.Int16Dtype | pd.Int32Dtype @@ -1061,7 +1062,6 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): | type[np.uint64] | type[np.intp] | type[np.uintp] - | type[np.integer] | type[np.byte] | type[np.ubyte], copy: _bool = ..., @@ -1089,7 +1089,6 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): | type[np.float16] | type[np.float32] | type[np.float64] - | type[np.floating] | type[float] | Literal["float"], copy: _bool = ..., @@ -1119,7 +1118,6 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[Timedelta]: ... - # ) -> TimedeltaSeries: ... @overload def astype( self, @@ -1127,7 +1125,6 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[Timestamp]: ... - # ) -> TimestampSeries: ... @overload def astype( self, diff --git a/tests/test_series.py b/tests/test_series.py index d9fabddac..c9f191a72 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -1434,50 +1434,54 @@ def test_updated_astype() -> None: s = pd.Series([3, 4, 5]) s1 = pd.Series(True) - check(assert_type(s.astype(int), "Series[int]"), pd.Series, int) - check(assert_type(s.astype("int"), "Series[int]"), pd.Series, int) - check(assert_type(s.astype("int32"), "Series[int]"), pd.Series, int) - check(assert_type(s.astype(bool), "Series[int]"), pd.Series, int) + check(assert_type(s.astype(int), "Series[int]"), pd.Series) + check(assert_type(s.astype("int"), "Series[int]"), pd.Series) + check(assert_type(s.astype("int32"), "Series[int]"), pd.Series) + check(assert_type(s.astype(bool), "Series[int]"), pd.Series) check(assert_type(s.astype(pd.Int8Dtype()), "Series[int]"), pd.Series) check(assert_type(s.astype(pd.Int16Dtype()), "Series[int]"), pd.Series) check(assert_type(s.astype(pd.Int32Dtype()), "Series[int]"), pd.Series) check(assert_type(s.astype(pd.Int64Dtype()), "Series[int]"), pd.Series) - check(assert_type(s.astype(np.int8), "Series[int]"), pd.Series, int) - check(assert_type(s.astype(np.int16), "Series[int]"), pd.Series, int) - check(assert_type(s.astype(np.int32), "Series[int]"), pd.Series, int) - check(assert_type(s.astype(np.int64), "Series[int]"), pd.Series, int) - check(assert_type(s.astype(np.uint8), "Series[int]"), pd.Series, int) - check(assert_type(s.astype(np.uint16), "Series[int]"), pd.Series, int) - check(assert_type(s.astype(np.uint32), "Series[int]"), pd.Series, int) - check(assert_type(s.astype(np.uint64), "Series[int]"), pd.Series, int) - check(assert_type(s.astype(np.intp), "Series[int]"), pd.Series, int) - check(assert_type(s.astype(np.uintp), "Series[int]"), pd.Series, int) - check(assert_type(s.astype(np.integer), "Series[int]"), pd.Series) - check(assert_type(s.astype(np.byte), "Series[int]"), pd.Series, int) - check(assert_type(s.astype(np.ubyte), "Series[int]"), pd.Series, int) + check(assert_type(s.astype(np.int8), "Series[int]"), pd.Series, np.int8) + check(assert_type(s.astype(np.int16), "Series[int]"), pd.Series, np.int16) + check(assert_type(s.astype(np.int32), "Series[int]"), pd.Series, np.int32) + check(assert_type(s.astype(np.int64), "Series[int]"), pd.Series, np.int64) + check(assert_type(s.astype(np.uint8), "Series[int]"), pd.Series, np.uint8) + check(assert_type(s.astype(np.uint16), "Series[int]"), pd.Series, np.uint16) + check(assert_type(s.astype(np.uint32), "Series[int]"), pd.Series, np.uint32) + check(assert_type(s.astype(np.uint64), "Series[int]"), pd.Series, np.uint64) + check(assert_type(s.astype(np.intp), "Series[int]"), pd.Series, np.intp) + check(assert_type(s.astype(np.uintp), "Series[int]"), pd.Series, np.uintp) + check(assert_type(s.astype(np.byte), "Series[int]"), pd.Series, np.byte) + check(assert_type(s.astype(np.ubyte), "Series[int]"), pd.Series, np.ubyte) check(assert_type(s.astype(str), "Series[str]"), pd.Series, str) check(assert_type(s.astype(pd.StringDtype()), "Series[str]"), pd.Series) check(assert_type(s.astype("str"), "Series[str]"), pd.Series, str) - check(assert_type(s.astype(bytes), "Series[bytes]"), pd.Series) + check(assert_type(s.astype(bytes), "Series[bytes]"), pd.Series, bytes) check(assert_type(s.astype(pd.Float32Dtype()), "Series[float]"), pd.Series) check(assert_type(s.astype(pd.Float64Dtype()), "Series[float]"), pd.Series) - check(assert_type(s.astype(np.float16), "Series[float]"), pd.Series, float) - check(assert_type(s.astype(np.float32), "Series[float]"), pd.Series, float) - check(assert_type(s.astype(np.float64), "Series[float]"), pd.Series, float) - check(assert_type(s.astype(np.floating), "Series[float]"), pd.Series) + check(assert_type(s.astype(np.float16), "Series[float]"), pd.Series, np.float16) + check(assert_type(s.astype(np.float32), "Series[float]"), pd.Series, np.float32) + check(assert_type(s.astype(np.float64), "Series[float]"), pd.Series, np.float64) check(assert_type(s.astype(float), "Series[float]"), pd.Series, float) check(assert_type(s.astype("float"), "Series[float]"), pd.Series, float) - check(assert_type(s.astype(np.complex64), "Series[complex]"), pd.Series, complex) - check(assert_type(s.astype(np.complex128), "Series[complex]"), pd.Series, complex) + check( + assert_type(s.astype(np.complex64), "Series[complex]"), pd.Series, np.complex64 + ) + check( + assert_type(s.astype(np.complex128), "Series[complex]"), + pd.Series, + np.complex128, + ) check(assert_type(s.astype(complex), "Series[complex]"), pd.Series, complex) check(assert_type(s.astype("complex"), "Series[complex]"), pd.Series, complex) check(assert_type(s1.astype(pd.BooleanDtype()), "Series[bool]"), pd.Series) - check(assert_type(s.astype("bool"), "Series[bool]"), pd.Series, bool) + check(assert_type(s.astype("bool"), "Series[bool]"), pd.Series, np.bool_) check( assert_type(s.astype("timedelta64[ns]"), "Series[Timedelta]"), From 3406aa0c2b581b81d14076487ff0d9964573a898 Mon Sep 17 00:00:00 2001 From: ram vikram singh Date: Thu, 23 Feb 2023 22:32:18 +0530 Subject: [PATCH 19/32] Delete table.rst --- table.rst | 46 ---------------------------------------------- 1 file changed, 46 deletions(-) delete mode 100644 table.rst diff --git a/table.rst b/table.rst deleted file mode 100644 index f049c7926..000000000 --- a/table.rst +++ /dev/null @@ -1,46 +0,0 @@ - -Table for astype - - -+-------------------+-------------------------+ -| S1 | Dtype | -+===================+=========================+ -| int | pd.Int8Dtype, | -| | pd.Int16Dtype, | -| | pd.Int32Dtype, | -| | pd.Int54Dtype, | -| | "int",int,np.int8, | -| | np.int16,np.int32, | -| | np.int64,np.uint8 | -| | np.uint16,np.uint32 | -| | np.uint64,np.intp | -| | np.uintp,np.integer | -+-------------------+-------------------------+ -| str | StringDtype,str,"str" | -+-------------------+-------------------------+ -| bytes | np.byte,np.ubyte,bytes | -| | "bytes" | -+-------------------+-------------------------+ -| float | pd.Float32Dtype, | -| | pd.Float64Dtype, | -| | float,"float",np.float16| -| | np.float32,np.float64 | -| | np.float96,np.float128 | -| | np.floating | -+-------------------+-------------------------+ -| complex | "complex",np.complex64, | -| | np.complex128, | -| | np.complex192, | -| | np.complex256, | -| | complex | -+-------------------+-------------------------+ -| bool | pd.BooleanDtype, bool | -| | np.bool_ | -| | "bool" | -+-------------------+-------------------------+ -| Timedelta | | -| | | -+-------------------+-------------------------+ -| Timestamp | "datetime64[ns]" | -+-------------------+-------------------------+ - From 5508f4a062bf12dd636ad7b5e7c83f7b74a34112 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Fri, 24 Feb 2023 01:48:55 +0530 Subject: [PATCH 20/32] added the bool overload to top and done the required test changes --- pandas-stubs/core/series.pyi | 23 +++++++++++------------ tests/test_series.py | 34 ++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 2bcb18cd5..c9f542a4a 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1043,11 +1043,17 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): ignore_index: _bool = ..., ) -> Series[S1]: ... @overload + def astype( # type: ignore[misc] + self, + dtype: type[bool] | pd.BooleanDtype | Literal["bool"], + copy: _bool = ..., + errors: IgnoreRaise = ..., + ) -> Series[bool]: ... + @overload def astype( self, - dtype: Literal["int", "int32"] - | type[int] - | type[bool] + dtype: Literal["int", "int32"] | type[int] + # | type[bool] | pd.Int8Dtype | pd.Int16Dtype | pd.Int32Dtype @@ -1105,26 +1111,19 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): errors: IgnoreRaise = ..., ) -> Series[complex]: ... @overload - def astype( - self, - dtype: pd.BooleanDtype | Literal["bool"], - copy: _bool = ..., - errors: IgnoreRaise = ..., - ) -> Series[bool]: ... - @overload def astype( self, dtype: Literal["timedelta64[ns]"], copy: _bool = ..., errors: IgnoreRaise = ..., - ) -> Series[Timedelta]: ... + ) -> TimedeltaSeries: ... @overload def astype( self, dtype: Literal["datetime64[ns]"], copy: _bool = ..., errors: IgnoreRaise = ..., - ) -> Series[Timestamp]: ... + ) -> TimestampSeries: ... @overload def astype( self, diff --git a/tests/test_series.py b/tests/test_series.py index c9f191a72..77971c3d3 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -24,7 +24,11 @@ ExtensionArray, ExtensionDtype, ) -from pandas.core.series import Series # noqa: F401 +from pandas.core.series import ( # noqa: F401 + Series, + TimedeltaSeries, + TimestampSeries, +) from pandas.core.window import ExponentialMovingWindow import pytest from typing_extensions import assert_type @@ -1437,11 +1441,10 @@ def test_updated_astype() -> None: check(assert_type(s.astype(int), "Series[int]"), pd.Series) check(assert_type(s.astype("int"), "Series[int]"), pd.Series) check(assert_type(s.astype("int32"), "Series[int]"), pd.Series) - check(assert_type(s.astype(bool), "Series[int]"), pd.Series) - check(assert_type(s.astype(pd.Int8Dtype()), "Series[int]"), pd.Series) - check(assert_type(s.astype(pd.Int16Dtype()), "Series[int]"), pd.Series) - check(assert_type(s.astype(pd.Int32Dtype()), "Series[int]"), pd.Series) - check(assert_type(s.astype(pd.Int64Dtype()), "Series[int]"), pd.Series) + check(assert_type(s.astype(pd.Int8Dtype()), "Series[int]"), pd.Series, np.int8) + check(assert_type(s.astype(pd.Int16Dtype()), "Series[int]"), pd.Series, np.int16) + check(assert_type(s.astype(pd.Int32Dtype()), "Series[int]"), pd.Series, np.int32) + check(assert_type(s.astype(pd.Int64Dtype()), "Series[int]"), pd.Series, np.int64) check(assert_type(s.astype(np.int8), "Series[int]"), pd.Series, np.int8) check(assert_type(s.astype(np.int16), "Series[int]"), pd.Series, np.int16) check(assert_type(s.astype(np.int32), "Series[int]"), pd.Series, np.int32) @@ -1456,13 +1459,17 @@ def test_updated_astype() -> None: check(assert_type(s.astype(np.ubyte), "Series[int]"), pd.Series, np.ubyte) check(assert_type(s.astype(str), "Series[str]"), pd.Series, str) - check(assert_type(s.astype(pd.StringDtype()), "Series[str]"), pd.Series) + check(assert_type(s.astype(pd.StringDtype()), "Series[str]"), pd.Series, str) check(assert_type(s.astype("str"), "Series[str]"), pd.Series, str) check(assert_type(s.astype(bytes), "Series[bytes]"), pd.Series, bytes) - check(assert_type(s.astype(pd.Float32Dtype()), "Series[float]"), pd.Series) - check(assert_type(s.astype(pd.Float64Dtype()), "Series[float]"), pd.Series) + check( + assert_type(s.astype(pd.Float32Dtype()), "Series[float]"), pd.Series, np.float32 + ) + check( + assert_type(s.astype(pd.Float64Dtype()), "Series[float]"), pd.Series, np.float64 + ) check(assert_type(s.astype(np.float16), "Series[float]"), pd.Series, np.float16) check(assert_type(s.astype(np.float32), "Series[float]"), pd.Series, np.float32) check(assert_type(s.astype(np.float64), "Series[float]"), pd.Series, np.float64) @@ -1480,17 +1487,20 @@ def test_updated_astype() -> None: check(assert_type(s.astype(complex), "Series[complex]"), pd.Series, complex) check(assert_type(s.astype("complex"), "Series[complex]"), pd.Series, complex) - check(assert_type(s1.astype(pd.BooleanDtype()), "Series[bool]"), pd.Series) + check( + assert_type(s1.astype(pd.BooleanDtype()), "Series[bool]"), pd.Series, np.bool_ + ) check(assert_type(s.astype("bool"), "Series[bool]"), pd.Series, np.bool_) + check(assert_type(s.astype(bool), "Series[bool]"), pd.Series, np.bool_) check( - assert_type(s.astype("timedelta64[ns]"), "Series[Timedelta]"), + assert_type(s.astype("timedelta64[ns]"), "TimedeltaSeries"), pd.Series, Timedelta, ) check( - assert_type(s.astype("datetime64[ns]"), "Series[Timestamp]"), + assert_type(s.astype("datetime64[ns]"), "TimestampSeries"), pd.Series, Timestamp, ) From fefdd839e1836e0561d0f8e04411b8c76dfbde97 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Fri, 24 Feb 2023 13:38:31 +0530 Subject: [PATCH 21/32] added type_checker --- tests/test_series.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/tests/test_series.py b/tests/test_series.py index 77971c3d3..a6c787997 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -24,14 +24,13 @@ ExtensionArray, ExtensionDtype, ) -from pandas.core.series import ( # noqa: F401 - Series, - TimedeltaSeries, - TimestampSeries, -) +from pandas.core.series import Series # noqa: F401 from pandas.core.window import ExponentialMovingWindow import pytest -from typing_extensions import assert_type +from typing_extensions import ( + TypeAlias, + assert_type, +) import xarray as xr from pandas._libs.tslibs.timedeltas import Timedelta @@ -48,6 +47,15 @@ pytest_warns_bounded, ) +if TYPE_CHECKING: + from pandas.core.series import ( # noqa: F401 + TimedeltaSeries, + TimestampSeries, + ) +else: + TimedeltaSeries: TypeAlias = pd.Series # noqa: F401 + TimestampSeries: TypeAlias = pd.Series # noqa: F401 + if TYPE_CHECKING: from pandas._typing import np_ndarray_int # noqa: F401 From 691864327be6aced33325adfd3db16a9657704cd Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Fri, 24 Feb 2023 20:13:40 +0530 Subject: [PATCH 22/32] added types for check and did requested changes --- pandas-stubs/core/series.pyi | 6 +-- tests/test_series.py | 96 +++++++++++++++++++----------------- 2 files changed, 55 insertions(+), 47 deletions(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index c9f542a4a..2eb5f7191 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1045,15 +1045,15 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): @overload def astype( # type: ignore[misc] self, - dtype: type[bool] | pd.BooleanDtype | Literal["bool"], + dtype: type[bool] | type[np.bool_] | pd.BooleanDtype | Literal["bool"], copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[bool]: ... @overload def astype( self, - dtype: Literal["int", "int32"] | type[int] - # | type[bool] + dtype: Literal["int", "int32"] + | type[int] | pd.Int8Dtype | pd.Int16Dtype | pd.Int32Dtype diff --git a/tests/test_series.py b/tests/test_series.py index a6c787997..823c8557c 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -24,7 +24,6 @@ ExtensionArray, ExtensionDtype, ) -from pandas.core.series import Series # noqa: F401 from pandas.core.window import ExponentialMovingWindow import pytest from typing_extensions import ( @@ -53,8 +52,8 @@ TimestampSeries, ) else: - TimedeltaSeries: TypeAlias = pd.Series # noqa: F401 - TimestampSeries: TypeAlias = pd.Series # noqa: F401 + TimedeltaSeries: TypeAlias = pd.Series + TimestampSeries: TypeAlias = pd.Series if TYPE_CHECKING: from pandas._typing import np_ndarray_int # noqa: F401 @@ -1446,69 +1445,78 @@ def test_updated_astype() -> None: s = pd.Series([3, 4, 5]) s1 = pd.Series(True) - check(assert_type(s.astype(int), "Series[int]"), pd.Series) - check(assert_type(s.astype("int"), "Series[int]"), pd.Series) - check(assert_type(s.astype("int32"), "Series[int]"), pd.Series) - check(assert_type(s.astype(pd.Int8Dtype()), "Series[int]"), pd.Series, np.int8) - check(assert_type(s.astype(pd.Int16Dtype()), "Series[int]"), pd.Series, np.int16) - check(assert_type(s.astype(pd.Int32Dtype()), "Series[int]"), pd.Series, np.int32) - check(assert_type(s.astype(pd.Int64Dtype()), "Series[int]"), pd.Series, np.int64) - check(assert_type(s.astype(np.int8), "Series[int]"), pd.Series, np.int8) - check(assert_type(s.astype(np.int16), "Series[int]"), pd.Series, np.int16) - check(assert_type(s.astype(np.int32), "Series[int]"), pd.Series, np.int32) - check(assert_type(s.astype(np.int64), "Series[int]"), pd.Series, np.int64) - check(assert_type(s.astype(np.uint8), "Series[int]"), pd.Series, np.uint8) - check(assert_type(s.astype(np.uint16), "Series[int]"), pd.Series, np.uint16) - check(assert_type(s.astype(np.uint32), "Series[int]"), pd.Series, np.uint32) - check(assert_type(s.astype(np.uint64), "Series[int]"), pd.Series, np.uint64) - check(assert_type(s.astype(np.intp), "Series[int]"), pd.Series, np.intp) - check(assert_type(s.astype(np.uintp), "Series[int]"), pd.Series, np.uintp) - check(assert_type(s.astype(np.byte), "Series[int]"), pd.Series, np.byte) - check(assert_type(s.astype(np.ubyte), "Series[int]"), pd.Series, np.ubyte) - - check(assert_type(s.astype(str), "Series[str]"), pd.Series, str) - check(assert_type(s.astype(pd.StringDtype()), "Series[str]"), pd.Series, str) - check(assert_type(s.astype("str"), "Series[str]"), pd.Series, str) - - check(assert_type(s.astype(bytes), "Series[bytes]"), pd.Series, bytes) + check(assert_type(s.astype(int), "pd.Series[int]"), pd.Series, np.int32) + check(assert_type(s.astype("int"), "pd.Series[int]"), pd.Series, np.int32) + check(assert_type(s.astype("int32"), "pd.Series[int]"), pd.Series, np.int32) + check(assert_type(s.astype(pd.Int8Dtype()), "pd.Series[int]"), pd.Series, np.int8) + check(assert_type(s.astype(pd.Int16Dtype()), "pd.Series[int]"), pd.Series, np.int16) + check(assert_type(s.astype(pd.Int32Dtype()), "pd.Series[int]"), pd.Series, np.int32) + check(assert_type(s.astype(pd.Int64Dtype()), "pd.Series[int]"), pd.Series, np.int64) + check(assert_type(s.astype(np.int8), "pd.Series[int]"), pd.Series, np.int8) + check(assert_type(s.astype(np.int16), "pd.Series[int]"), pd.Series, np.int16) + check(assert_type(s.astype(np.int32), "pd.Series[int]"), pd.Series, np.int32) + check(assert_type(s.astype(np.int64), "pd.Series[int]"), pd.Series, np.int64) + check(assert_type(s.astype(np.uint8), "pd.Series[int]"), pd.Series, np.uint8) + check(assert_type(s.astype(np.uint16), "pd.Series[int]"), pd.Series, np.uint16) + check(assert_type(s.astype(np.uint32), "pd.Series[int]"), pd.Series, np.uint32) + check(assert_type(s.astype(np.uint64), "pd.Series[int]"), pd.Series, np.uint64) + check(assert_type(s.astype(np.intp), "pd.Series[int]"), pd.Series, np.intp) + check(assert_type(s.astype(np.uintp), "pd.Series[int]"), pd.Series, np.uintp) + check(assert_type(s.astype(np.byte), "pd.Series[int]"), pd.Series, np.byte) + check(assert_type(s.astype(np.ubyte), "pd.Series[int]"), pd.Series, np.ubyte) + + check(assert_type(s.astype(str), "pd.Series[str]"), pd.Series, str) + check(assert_type(s.astype(pd.StringDtype()), "pd.Series[str]"), pd.Series, str) + check(assert_type(s.astype("str"), "pd.Series[str]"), pd.Series, str) + + check(assert_type(s.astype(bytes), "pd.Series[bytes]"), pd.Series, bytes) check( - assert_type(s.astype(pd.Float32Dtype()), "Series[float]"), pd.Series, np.float32 + assert_type(s.astype(pd.Float32Dtype()), "pd.Series[float]"), + pd.Series, + np.float32, ) check( - assert_type(s.astype(pd.Float64Dtype()), "Series[float]"), pd.Series, np.float64 + assert_type(s.astype(pd.Float64Dtype()), "pd.Series[float]"), + pd.Series, + np.float64, ) - check(assert_type(s.astype(np.float16), "Series[float]"), pd.Series, np.float16) - check(assert_type(s.astype(np.float32), "Series[float]"), pd.Series, np.float32) - check(assert_type(s.astype(np.float64), "Series[float]"), pd.Series, np.float64) - check(assert_type(s.astype(float), "Series[float]"), pd.Series, float) - check(assert_type(s.astype("float"), "Series[float]"), pd.Series, float) + check(assert_type(s.astype(np.float16), "pd.Series[float]"), pd.Series, np.float16) + check(assert_type(s.astype(np.float32), "pd.Series[float]"), pd.Series, np.float32) + check(assert_type(s.astype(np.float64), "pd.Series[float]"), pd.Series, np.float64) + check(assert_type(s.astype(float), "pd.Series[float]"), pd.Series, float) + check(assert_type(s.astype("float"), "pd.Series[float]"), pd.Series, float) check( - assert_type(s.astype(np.complex64), "Series[complex]"), pd.Series, np.complex64 + assert_type(s.astype(np.complex64), "pd.Series[complex]"), + pd.Series, + np.complex64, ) check( - assert_type(s.astype(np.complex128), "Series[complex]"), + assert_type(s.astype(np.complex128), "pd.Series[complex]"), pd.Series, np.complex128, ) - check(assert_type(s.astype(complex), "Series[complex]"), pd.Series, complex) - check(assert_type(s.astype("complex"), "Series[complex]"), pd.Series, complex) + check(assert_type(s.astype(complex), "pd.Series[complex]"), pd.Series, complex) + check(assert_type(s.astype("complex"), "pd.Series[complex]"), pd.Series, complex) check( - assert_type(s1.astype(pd.BooleanDtype()), "Series[bool]"), pd.Series, np.bool_ + assert_type(s1.astype(pd.BooleanDtype()), "pd.Series[bool]"), + pd.Series, + np.bool_, ) - check(assert_type(s.astype("bool"), "Series[bool]"), pd.Series, np.bool_) - check(assert_type(s.astype(bool), "Series[bool]"), pd.Series, np.bool_) + check(assert_type(s.astype("bool"), "pd.Series[bool]"), pd.Series, np.bool_) + check(assert_type(s.astype(bool), "pd.Series[bool]"), pd.Series, np.bool_) + check(assert_type(s.astype(np.bool_), "pd.Series[bool]"), pd.Series, np.bool_) check( - assert_type(s.astype("timedelta64[ns]"), "TimedeltaSeries"), + assert_type(s.astype("timedelta64[ns]"), TimedeltaSeries), pd.Series, Timedelta, ) check( - assert_type(s.astype("datetime64[ns]"), "TimestampSeries"), + assert_type(s.astype("datetime64[ns]"), TimestampSeries), pd.Series, Timestamp, ) From a80df88901c84a29efe8a632782c061ec9540d43 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Fri, 24 Feb 2023 20:18:54 +0530 Subject: [PATCH 23/32] updated the check types --- tests/test_series.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_series.py b/tests/test_series.py index 823c8557c..785141275 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -1445,8 +1445,8 @@ def test_updated_astype() -> None: s = pd.Series([3, 4, 5]) s1 = pd.Series(True) - check(assert_type(s.astype(int), "pd.Series[int]"), pd.Series, np.int32) - check(assert_type(s.astype("int"), "pd.Series[int]"), pd.Series, np.int32) + check(assert_type(s.astype(int), "pd.Series[int]"), pd.Series, np.integer) + check(assert_type(s.astype("int"), "pd.Series[int]"), pd.Series, np.integer) check(assert_type(s.astype("int32"), "pd.Series[int]"), pd.Series, np.int32) check(assert_type(s.astype(pd.Int8Dtype()), "pd.Series[int]"), pd.Series, np.int8) check(assert_type(s.astype(pd.Int16Dtype()), "pd.Series[int]"), pd.Series, np.int16) From f43a5e81aa6645bad6d19659b914df51c7439fc4 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Sat, 25 Feb 2023 16:40:35 +0530 Subject: [PATCH 24/32] added astype in dataframe and other changes --- pandas-stubs/_typing.pyi | 61 ++++++++++++++++++++++++++++++++-- pandas-stubs/core/frame.pyi | 11 ++++++- pandas-stubs/core/series.pyi | 63 ++++++++++++++++-------------------- tests/test_frame.py | 11 +++++++ tests/test_series.py | 4 +++ 5 files changed, 111 insertions(+), 39 deletions(-) diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index 1d482b508..3109f4f0f 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -19,12 +19,16 @@ from typing import ( import numpy as np from numpy import typing as npt +import pandas as pd from pandas.core.arrays import ExtensionArray from pandas.core.frame import DataFrame from pandas.core.generic import NDFrame from pandas.core.groupby.grouper import Grouper from pandas.core.indexes.base import Index -from pandas.core.series import Series +from pandas.core.series import ( + Series, + _str, +) from typing_extensions import TypeAlias from pandas._libs.interval import Interval @@ -75,7 +79,19 @@ class FulldatetimeDict(YearMonthDayDict, total=False): # dtypes NpDtype: TypeAlias = str | np.dtype[np.generic] | type[str | complex | bool | object] Dtype: TypeAlias = ExtensionDtype | NpDtype -AstypeArg: TypeAlias = ExtensionDtype | npt.DTypeLike +# AstypeArg: TypeAlias = ExtensionDtype | npt.DTypeLike +AstypeArg: TypeAlias = ( + BooleanDtypeArg + | IntDtypeArg + | StrDtypeArg + | BytesDtypeArg + | FloatDtypeArg + | ComplexDtypeArg + | TimedeltaDtypeArg + | TimestampDtypeArg + | CategoricalDtype + | ExtensionDtype +) # DtypeArg specifies all allowable dtypes in a functions its dtype argument DtypeArg: TypeAlias = Dtype | dict[Any, Dtype] DtypeObj: TypeAlias = np.dtype[np.generic] | ExtensionDtype @@ -378,3 +394,44 @@ RandomState: TypeAlias = ( ) __all__ = ["npt", "type_t"] + +BooleanDtypeArg: TypeAlias = ( + type[bool] | type[np.bool_] | pd.BooleanDtype | Literal["bool"] +) +IntDtypeArg: TypeAlias = ( + Literal["int", "int32"] + | type[int] + | pd.Int8Dtype + | pd.Int16Dtype + | pd.Int32Dtype + | pd.Int64Dtype + | type[np.int8] + | type[np.int16] + | type[np.int32] + | type[np.int64] + | type[np.uint8] + | type[np.uint16] + | type[np.uint32] + | type[np.uint64] + | type[np.intp] + | type[np.uintp] + | type[np.byte] + | type[np.ubyte] +) +StrDtypeArg: TypeAlias = type[_str] | pd.StringDtype | Literal["str"] +BytesDtypeArg: TypeAlias = type[bytes] +FloatDtypeArg: TypeAlias = ( + pd.Float32Dtype + | pd.Float64Dtype + | type[np.float16] + | type[np.float32] + | type[np.float64] + | type[float] + | Literal["float"] +) +ComplexDtypeArg: TypeAlias = ( + type[np.complex64] | type[np.complex128] | type[complex] | Literal["complex"] +) +TimedeltaDtypeArg: TypeAlias = Literal["timedelta64[ns]"] +TimestampDtypeArg: TypeAlias = Literal["datetime64[ns]"] +CategoryDtypeArg: TypeAlias = Literal["category"] diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 664803b81..161cb9bec 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -59,6 +59,7 @@ from pandas._typing import ( AggFuncTypeFrame, AnyArrayLike, ArrayLike, + AstypeArg, Axes, Axis, AxisType, @@ -1438,11 +1439,19 @@ class DataFrame(NDFrame, OpsMixin): normalize: _bool = ..., fill_value: Scalar | None = ..., ) -> DataFrame: ... + @overload + def astype( + self, + dtype: AstypeArg | dict[Any, AstypeArg], + copy: _bool = ..., + errors: IgnoreRaise = ..., + ) -> DataFrame: ... + @overload def astype( self, dtype: _str | Dtype | Mapping[HashableT, _str | Dtype] | Series, copy: _bool = ..., - errors: _str = ..., + errors: IgnoreRaise = ..., ) -> DataFrame: ... def at_time( self, diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 2eb5f7191..79a6f2a18 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -31,7 +31,6 @@ from matplotlib.axes import ( SubplotBase, ) import numpy as np -import pandas as pd from pandas import ( Period, Timedelta, @@ -87,17 +86,23 @@ from pandas._typing import ( Axes, Axis, AxisType, + BooleanDtypeArg, + BytesDtypeArg, CalculationMethod, + CategoryDtypeArg, + ComplexDtypeArg, CompressionOptions, DtypeObj, FilePath, FillnaOptions, + FloatDtypeArg, GroupByObjectNonScalar, HashableT1, HashableT2, HashableT3, IgnoreRaise, IndexingInt, + IntDtypeArg, IntervalClosedType, JoinHow, JsonSeriesOrient, @@ -113,7 +118,10 @@ from pandas._typing import ( Scalar, SeriesAxisType, SortKind, + StrDtypeArg, + TimedeltaDtypeArg, TimestampConvention, + TimestampDtypeArg, WriteBuffer, np_ndarray_anyint, np_ndarray_bool, @@ -121,6 +129,8 @@ from pandas._typing import ( num, ) +from pandas.core.dtypes.base import ExtensionDtype + from pandas.plotting import PlotAccessor from .base import IndexOpsMixin @@ -1045,89 +1055,70 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): @overload def astype( # type: ignore[misc] self, - dtype: type[bool] | type[np.bool_] | pd.BooleanDtype | Literal["bool"], + dtype: BooleanDtypeArg, copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[bool]: ... @overload def astype( self, - dtype: Literal["int", "int32"] - | type[int] - | pd.Int8Dtype - | pd.Int16Dtype - | pd.Int32Dtype - | pd.Int64Dtype - | type[np.int8] - | type[np.int16] - | type[np.int32] - | type[np.int64] - | type[np.uint8] - | type[np.uint16] - | type[np.uint32] - | type[np.uint64] - | type[np.intp] - | type[np.uintp] - | type[np.byte] - | type[np.ubyte], + dtype: IntDtypeArg, copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[int]: ... @overload def astype( self, - dtype: type[_str] | pd.StringDtype | Literal["str"], + dtype: StrDtypeArg, copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[_str]: ... @overload def astype( self, - dtype: type[bytes], + dtype: BytesDtypeArg, copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[bytes]: ... @overload def astype( self, - dtype: pd.Float32Dtype - | pd.Float64Dtype - | type[np.float16] - | type[np.float32] - | type[np.float64] - | type[float] - | Literal["float"], + dtype: FloatDtypeArg, copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[float]: ... @overload def astype( self, - dtype: type[np.complex64] - | type[np.complex128] - | type[complex] - | Literal["complex"], + dtype: ComplexDtypeArg, copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series[complex]: ... @overload def astype( self, - dtype: Literal["timedelta64[ns]"], + dtype: TimedeltaDtypeArg, copy: _bool = ..., errors: IgnoreRaise = ..., ) -> TimedeltaSeries: ... @overload def astype( self, - dtype: Literal["datetime64[ns]"], + dtype: TimestampDtypeArg, copy: _bool = ..., errors: IgnoreRaise = ..., ) -> TimestampSeries: ... @overload def astype( self, - dtype: Literal["category"], + dtype: CategoryDtypeArg, + copy: _bool = ..., + errors: IgnoreRaise = ..., + ) -> Series: ... + @overload + def astype( + self, + dtype: ExtensionDtype, copy: _bool = ..., errors: IgnoreRaise = ..., ) -> Series: ... diff --git a/tests/test_frame.py b/tests/test_frame.py index 0e55e3f1b..af4ba6f41 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -2423,3 +2423,14 @@ def test_insert_newvalues() -> None: assert assert_type(df.insert(loc=0, column="b", value=None), None) is None assert assert_type(ab.insert(loc=0, column="newcol", value=[99, 99]), None) is None assert assert_type(ef.insert(loc=0, column="g", value=4), None) is None + + +def test_astype() -> None: + s = pd.DataFrame({"d": [1, 2]}) + + check(assert_type(s.astype(int), "pd.DataFrame"), pd.DataFrame) + check(assert_type(s.astype(pd.Int64Dtype()), "pd.DataFrame"), pd.DataFrame) + check(assert_type(s.astype(str), "pd.DataFrame"), pd.DataFrame) + check(assert_type(s.astype(bytes), "pd.DataFrame"), pd.DataFrame) + check(assert_type(s.astype(pd.Float64Dtype()), "pd.DataFrame"), pd.DataFrame) + check(assert_type(s.astype(complex), "pd.DataFrame"), pd.DataFrame) diff --git a/tests/test_series.py b/tests/test_series.py index 785141275..3608f7d47 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -17,6 +17,8 @@ cast, ) +# from decimal import Decimal +# from pandas.tests.extension.decimal import DecimalDtype import numpy as np import pandas as pd from pandas._testing import ensure_clean @@ -1520,3 +1522,5 @@ def test_updated_astype() -> None: pd.Series, Timestamp, ) + + # check(assert_type(pd.Series([Decimal(x) for x in [1,2,3]]).astype(DecimalDtype()), pd.Series), Decimal) From 3b86a87bab0f92edcb9c9484d515ddde860c7abe Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Sat, 25 Feb 2023 16:48:19 +0530 Subject: [PATCH 25/32] Update test_series.py --- tests/test_series.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_series.py b/tests/test_series.py index 3608f7d47..661275158 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -49,8 +49,7 @@ ) if TYPE_CHECKING: - from pandas.core.series import ( # noqa: F401 - TimedeltaSeries, + from pandas.core.series import ( TimestampSeries, ) else: From f6a8949ae940574392cd11dc628c19e59fb91907 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Sat, 25 Feb 2023 16:56:24 +0530 Subject: [PATCH 26/32] Update test_series.py --- tests/test_series.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_series.py b/tests/test_series.py index 661275158..b4fc74189 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -50,6 +50,7 @@ if TYPE_CHECKING: from pandas.core.series import ( + TimedeltaSeries, TimestampSeries, ) else: From 3259170b2d6b5d5e0e22e7b05b75b71ac919691e Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Sat, 25 Feb 2023 23:37:26 +0530 Subject: [PATCH 27/32] added dict test for astype in datatest_frame and tests for ExtensionDtype in test_series --- pandas-stubs/_typing.pyi | 8 ++------ pandas-stubs/core/frame.pyi | 8 -------- tests/test_frame.py | 11 +++++++---- tests/test_series.py | 13 ++++++++++--- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index 3109f4f0f..ab167ea5f 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -25,10 +25,7 @@ from pandas.core.frame import DataFrame from pandas.core.generic import NDFrame from pandas.core.groupby.grouper import Grouper from pandas.core.indexes.base import Index -from pandas.core.series import ( - Series, - _str, -) +from pandas.core.series import Series from typing_extensions import TypeAlias from pandas._libs.interval import Interval @@ -79,7 +76,6 @@ class FulldatetimeDict(YearMonthDayDict, total=False): # dtypes NpDtype: TypeAlias = str | np.dtype[np.generic] | type[str | complex | bool | object] Dtype: TypeAlias = ExtensionDtype | NpDtype -# AstypeArg: TypeAlias = ExtensionDtype | npt.DTypeLike AstypeArg: TypeAlias = ( BooleanDtypeArg | IntDtypeArg @@ -418,7 +414,7 @@ IntDtypeArg: TypeAlias = ( | type[np.byte] | type[np.ubyte] ) -StrDtypeArg: TypeAlias = type[_str] | pd.StringDtype | Literal["str"] +StrDtypeArg: TypeAlias = type[str] | pd.StringDtype | Literal["str"] BytesDtypeArg: TypeAlias = type[bytes] FloatDtypeArg: TypeAlias = ( pd.Float32Dtype diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 161cb9bec..793343362 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -1439,20 +1439,12 @@ class DataFrame(NDFrame, OpsMixin): normalize: _bool = ..., fill_value: Scalar | None = ..., ) -> DataFrame: ... - @overload def astype( self, dtype: AstypeArg | dict[Any, AstypeArg], copy: _bool = ..., errors: IgnoreRaise = ..., ) -> DataFrame: ... - @overload - def astype( - self, - dtype: _str | Dtype | Mapping[HashableT, _str | Dtype] | Series, - copy: _bool = ..., - errors: IgnoreRaise = ..., - ) -> DataFrame: ... def at_time( self, time: _str | datetime.time, diff --git a/tests/test_frame.py b/tests/test_frame.py index af4ba6f41..f02fc6a62 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -2269,7 +2269,7 @@ def test_df_accepting_dicts_iterator() -> None: def test_series_added_in_astype() -> None: # GH410 df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) - check(assert_type(df.astype(df.dtypes), pd.DataFrame), pd.DataFrame) + check(assert_type(df.astype({"a": int, "b": "int"}), pd.DataFrame), pd.DataFrame) def test_series_groupby_and_value_counts() -> None: @@ -2296,9 +2296,7 @@ def test_axes_as_tuple() -> None: def test_astype_dict() -> None: # GH 447 df = pd.DataFrame({"a": [1, 2, 3], 43: [4, 5, 6]}) - columns_types = {"a": "int", 43: "float"} - df = df.astype(columns_types) - check(assert_type(df, pd.DataFrame), pd.DataFrame) + check(assert_type(df.astype({"a": "int", 43: "float"}), pd.DataFrame), pd.DataFrame) def test_setitem_none() -> None: @@ -2427,6 +2425,7 @@ def test_insert_newvalues() -> None: def test_astype() -> None: s = pd.DataFrame({"d": [1, 2]}) + ab = pd.DataFrame({"col1": [1, 2], "col2": ["a", "b"]}) check(assert_type(s.astype(int), "pd.DataFrame"), pd.DataFrame) check(assert_type(s.astype(pd.Int64Dtype()), "pd.DataFrame"), pd.DataFrame) @@ -2434,3 +2433,7 @@ def test_astype() -> None: check(assert_type(s.astype(bytes), "pd.DataFrame"), pd.DataFrame) check(assert_type(s.astype(pd.Float64Dtype()), "pd.DataFrame"), pd.DataFrame) check(assert_type(s.astype(complex), "pd.DataFrame"), pd.DataFrame) + check( + assert_type(ab.astype({"col1": "int32", "col2": str}), "pd.DataFrame"), + pd.DataFrame, + ) diff --git a/tests/test_series.py b/tests/test_series.py index b4fc74189..7680bbd34 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -1,6 +1,7 @@ from __future__ import annotations import datetime +from decimal import Decimal from pathlib import Path import re from typing import ( @@ -17,8 +18,6 @@ cast, ) -# from decimal import Decimal -# from pandas.tests.extension.decimal import DecimalDtype import numpy as np import pandas as pd from pandas._testing import ensure_clean @@ -27,6 +26,7 @@ ExtensionDtype, ) from pandas.core.window import ExponentialMovingWindow +from pandas.tests.extension.decimal import DecimalDtype import pytest from typing_extensions import ( TypeAlias, @@ -1523,4 +1523,11 @@ def test_updated_astype() -> None: Timestamp, ) - # check(assert_type(pd.Series([Decimal(x) for x in [1,2,3]]).astype(DecimalDtype()), pd.Series), Decimal) + orseries = pd.Series([Decimal(x) for x in [1, 2, 3]]) + newtype: ExtensionDtype = DecimalDtype() + decseries = orseries.astype(newtype) + check( + assert_type(decseries, pd.Series), + pd.Series, + Decimal, + ) From 4090d31b11c0346bcd15c5157861080d8df0a23e Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Sat, 25 Feb 2023 23:52:23 +0530 Subject: [PATCH 28/32] commented out the decimal tests --- tests/test_series.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/test_series.py b/tests/test_series.py index 7680bbd34..fa9e96633 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -1,7 +1,7 @@ from __future__ import annotations import datetime -from decimal import Decimal +# from decimal import Decimal from pathlib import Path import re from typing import ( @@ -26,7 +26,7 @@ ExtensionDtype, ) from pandas.core.window import ExponentialMovingWindow -from pandas.tests.extension.decimal import DecimalDtype +# from pandas.tests.extension.decimal import DecimalDtype import pytest from typing_extensions import ( TypeAlias, @@ -1523,11 +1523,11 @@ def test_updated_astype() -> None: Timestamp, ) - orseries = pd.Series([Decimal(x) for x in [1, 2, 3]]) - newtype: ExtensionDtype = DecimalDtype() - decseries = orseries.astype(newtype) - check( - assert_type(decseries, pd.Series), - pd.Series, - Decimal, - ) + # orseries = pd.Series([Decimal(x) for x in [1, 2, 3]]) + # newtype: ExtensionDtype = DecimalDtype() + # decseries = orseries.astype(newtype) + # check( + # assert_type(decseries, pd.Series), + # pd.Series, + # Decimal, + # ) From 29540d38a3a4b116a7e26cdbc0709a63ca274a29 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Sat, 25 Feb 2023 23:54:52 +0530 Subject: [PATCH 29/32] Update test_series.py --- tests/test_series.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_series.py b/tests/test_series.py index fa9e96633..c1e5465c3 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -1,6 +1,7 @@ from __future__ import annotations import datetime + # from decimal import Decimal from pathlib import Path import re @@ -26,6 +27,7 @@ ExtensionDtype, ) from pandas.core.window import ExponentialMovingWindow + # from pandas.tests.extension.decimal import DecimalDtype import pytest from typing_extensions import ( From f46cc5226cf312b85435820c0c4f00aeb4d268df Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Sun, 26 Feb 2023 00:49:31 +0530 Subject: [PATCH 30/32] updated dtype args in astype --- pandas-stubs/core/frame.pyi | 3 ++- tests/test_frame.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 793343362..5670ac759 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -1441,7 +1441,8 @@ class DataFrame(NDFrame, OpsMixin): ) -> DataFrame: ... def astype( self, - dtype: AstypeArg | dict[Any, AstypeArg], + dtype: AstypeArg | Mapping[Hashable, AstypeArg] | Series, + # dtype: AstypeArg | dict[Any, AstypeArg], copy: _bool = ..., errors: IgnoreRaise = ..., ) -> DataFrame: ... diff --git a/tests/test_frame.py b/tests/test_frame.py index f02fc6a62..0e6d633f7 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -2269,7 +2269,7 @@ def test_df_accepting_dicts_iterator() -> None: def test_series_added_in_astype() -> None: # GH410 df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) - check(assert_type(df.astype({"a": int, "b": "int"}), pd.DataFrame), pd.DataFrame) + check(assert_type(df.astype(df.dtypes), pd.DataFrame), pd.DataFrame) def test_series_groupby_and_value_counts() -> None: From b9f5adb5e04a217831a8f0d7d82d835f96b49da2 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Sun, 26 Feb 2023 02:36:11 +0530 Subject: [PATCH 31/32] added any to list of args for astype --- pandas-stubs/_typing.pyi | 2 +- pandas-stubs/core/frame.pyi | 3 +-- tests/test_frame.py | 3 +++ 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index ab167ea5f..836bc670e 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -76,6 +76,7 @@ class FulldatetimeDict(YearMonthDayDict, total=False): # dtypes NpDtype: TypeAlias = str | np.dtype[np.generic] | type[str | complex | bool | object] Dtype: TypeAlias = ExtensionDtype | NpDtype +DtypeArg: TypeAlias = Dtype | dict[Any, Dtype] AstypeArg: TypeAlias = ( BooleanDtypeArg | IntDtypeArg @@ -89,7 +90,6 @@ AstypeArg: TypeAlias = ( | ExtensionDtype ) # DtypeArg specifies all allowable dtypes in a functions its dtype argument -DtypeArg: TypeAlias = Dtype | dict[Any, Dtype] DtypeObj: TypeAlias = np.dtype[np.generic] | ExtensionDtype # filenames and file-like-objects diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 5670ac759..a48643e8e 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -1441,8 +1441,7 @@ class DataFrame(NDFrame, OpsMixin): ) -> DataFrame: ... def astype( self, - dtype: AstypeArg | Mapping[Hashable, AstypeArg] | Series, - # dtype: AstypeArg | dict[Any, AstypeArg], + dtype: AstypeArg | Mapping[Hashable, AstypeArg] | Series | Any, copy: _bool = ..., errors: IgnoreRaise = ..., ) -> DataFrame: ... diff --git a/tests/test_frame.py b/tests/test_frame.py index 0e6d633f7..3b907569b 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -2296,6 +2296,9 @@ def test_axes_as_tuple() -> None: def test_astype_dict() -> None: # GH 447 df = pd.DataFrame({"a": [1, 2, 3], 43: [4, 5, 6]}) + columns_types = {"a": "int", 43: "float"} + de = df.astype(columns_types) + check(assert_type(de, pd.DataFrame), pd.DataFrame) check(assert_type(df.astype({"a": "int", 43: "float"}), pd.DataFrame), pd.DataFrame) From 7879eded88621a6e55a615b073de4e6a60c1f461 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Sun, 26 Feb 2023 03:49:31 +0530 Subject: [PATCH 32/32] changed dtype args --- pandas-stubs/_typing.pyi | 82 ++++++++++++++++++------------------- pandas-stubs/core/frame.pyi | 2 +- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index 836bc670e..476a76db1 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -77,6 +77,47 @@ class FulldatetimeDict(YearMonthDayDict, total=False): NpDtype: TypeAlias = str | np.dtype[np.generic] | type[str | complex | bool | object] Dtype: TypeAlias = ExtensionDtype | NpDtype DtypeArg: TypeAlias = Dtype | dict[Any, Dtype] +BooleanDtypeArg: TypeAlias = ( + type[bool] | type[np.bool_] | pd.BooleanDtype | Literal["bool"] +) +IntDtypeArg: TypeAlias = ( + Literal["int", "int32"] + | type[int] + | pd.Int8Dtype + | pd.Int16Dtype + | pd.Int32Dtype + | pd.Int64Dtype + | type[np.int8] + | type[np.int16] + | type[np.int32] + | type[np.int64] + | type[np.uint8] + | type[np.uint16] + | type[np.uint32] + | type[np.uint64] + | type[np.intp] + | type[np.uintp] + | type[np.byte] + | type[np.ubyte] +) +StrDtypeArg: TypeAlias = type[str] | pd.StringDtype | Literal["str"] +BytesDtypeArg: TypeAlias = type[bytes] +FloatDtypeArg: TypeAlias = ( + pd.Float32Dtype + | pd.Float64Dtype + | type[np.float16] + | type[np.float32] + | type[np.float64] + | type[float] + | Literal["float"] +) +ComplexDtypeArg: TypeAlias = ( + type[np.complex64] | type[np.complex128] | type[complex] | Literal["complex"] +) +TimedeltaDtypeArg: TypeAlias = Literal["timedelta64[ns]"] +TimestampDtypeArg: TypeAlias = Literal["datetime64[ns]"] +CategoryDtypeArg: TypeAlias = Literal["category"] + AstypeArg: TypeAlias = ( BooleanDtypeArg | IntDtypeArg @@ -390,44 +431,3 @@ RandomState: TypeAlias = ( ) __all__ = ["npt", "type_t"] - -BooleanDtypeArg: TypeAlias = ( - type[bool] | type[np.bool_] | pd.BooleanDtype | Literal["bool"] -) -IntDtypeArg: TypeAlias = ( - Literal["int", "int32"] - | type[int] - | pd.Int8Dtype - | pd.Int16Dtype - | pd.Int32Dtype - | pd.Int64Dtype - | type[np.int8] - | type[np.int16] - | type[np.int32] - | type[np.int64] - | type[np.uint8] - | type[np.uint16] - | type[np.uint32] - | type[np.uint64] - | type[np.intp] - | type[np.uintp] - | type[np.byte] - | type[np.ubyte] -) -StrDtypeArg: TypeAlias = type[str] | pd.StringDtype | Literal["str"] -BytesDtypeArg: TypeAlias = type[bytes] -FloatDtypeArg: TypeAlias = ( - pd.Float32Dtype - | pd.Float64Dtype - | type[np.float16] - | type[np.float32] - | type[np.float64] - | type[float] - | Literal["float"] -) -ComplexDtypeArg: TypeAlias = ( - type[np.complex64] | type[np.complex128] | type[complex] | Literal["complex"] -) -TimedeltaDtypeArg: TypeAlias = Literal["timedelta64[ns]"] -TimestampDtypeArg: TypeAlias = Literal["datetime64[ns]"] -CategoryDtypeArg: TypeAlias = Literal["category"] diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index a48643e8e..5cf47ede9 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -1441,7 +1441,7 @@ class DataFrame(NDFrame, OpsMixin): ) -> DataFrame: ... def astype( self, - dtype: AstypeArg | Mapping[Hashable, AstypeArg] | Series | Any, + dtype: AstypeArg | Mapping[Any, Dtype] | Series, copy: _bool = ..., errors: IgnoreRaise = ..., ) -> DataFrame: ...