From 33a66b672027e913c5f6990a5e5fffe46b005099 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 29 Nov 2020 08:48:12 -0800 Subject: [PATCH 1/2] CLN: remove unreachable in maybe_cast_result --- pandas/core/dtypes/cast.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index fe40bc42887c4..9e2fe3b594ac7 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -297,7 +297,9 @@ def trans(x): return result -def maybe_cast_result(result, obj: "Series", numeric_only: bool = False, how: str = ""): +def maybe_cast_result( + result: ArrayLike, obj: "Series", numeric_only: bool = False, how: str = "" +): """ Try casting result to a different type if appropriate @@ -320,19 +322,20 @@ def maybe_cast_result(result, obj: "Series", numeric_only: bool = False, how: st dtype = obj.dtype dtype = maybe_cast_result_dtype(dtype, how) - if not is_scalar(result): - if ( - is_extension_array_dtype(dtype) - and not is_categorical_dtype(dtype) - and dtype.kind != "M" - ): - # We have to special case categorical so as not to upcast - # things like counts back to categorical - cls = dtype.construct_array_type() - result = maybe_cast_to_extension_array(cls, result, dtype=dtype) + assert not is_scalar(result) + + if ( + is_extension_array_dtype(dtype) + and not is_categorical_dtype(dtype) + and dtype.kind != "M" + ): + # We have to special case categorical so as not to upcast + # things like counts back to categorical + cls = dtype.construct_array_type() + result = maybe_cast_to_extension_array(cls, result, dtype=dtype) - elif numeric_only and is_numeric_dtype(dtype) or not numeric_only: - result = maybe_downcast_to_dtype(result, dtype) + elif numeric_only and is_numeric_dtype(dtype) or not numeric_only: + result = maybe_downcast_to_dtype(result, dtype) return result From f0a91ebd72e64922b134c35ca87e66aec650173c Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 29 Nov 2020 08:49:24 -0800 Subject: [PATCH 2/2] Annotate --- pandas/core/dtypes/cast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 9e2fe3b594ac7..cf81e6f173bdd 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -299,7 +299,7 @@ def trans(x): def maybe_cast_result( result: ArrayLike, obj: "Series", numeric_only: bool = False, how: str = "" -): +) -> ArrayLike: """ Try casting result to a different type if appropriate