44
44
)
45
45
from pandas ._libs .tslibs .timedeltas import array_to_timedelta64
46
46
from pandas ._typing import (
47
- AnyArrayLike ,
48
47
ArrayLike ,
49
48
Dtype ,
50
49
DtypeObj ,
@@ -407,27 +406,16 @@ def maybe_cast_result(
407
406
408
407
assert not is_scalar (result )
409
408
410
- if (
411
- is_extension_array_dtype (dtype )
412
- and not is_categorical_dtype (dtype )
413
- and dtype .kind != "M"
414
- ):
415
- # We have to special case categorical so as not to upcast
416
- # things like counts back to categorical
417
-
418
- # error: Item "dtype[Any]" of "Union[dtype[Any], ExtensionDtype]" has no
419
- # attribute "construct_array_type"
420
- cls = dtype .construct_array_type () # type: ignore[union-attr]
421
- # error: Argument "dtype" to "maybe_cast_to_extension_array" has incompatible
422
- # type "Union[dtype[Any], ExtensionDtype]"; expected "Optional[ExtensionDtype]"
423
- result = maybe_cast_to_extension_array (
424
- cls , result , dtype = dtype # type: ignore[arg-type]
425
- )
409
+ if isinstance (dtype , ExtensionDtype ):
410
+ if not is_categorical_dtype (dtype ) and dtype .kind != "M" :
411
+ # We have to special case categorical so as not to upcast
412
+ # things like counts back to categorical
426
413
427
- elif numeric_only and is_numeric_dtype (dtype ) or not numeric_only :
428
- # error: Argument 2 to "maybe_downcast_to_dtype" has incompatible type
429
- # "Union[dtype[Any], ExtensionDtype]"; expected "Union[str, dtype[Any]]"
430
- result = maybe_downcast_to_dtype (result , dtype ) # type: ignore[arg-type]
414
+ cls = dtype .construct_array_type ()
415
+ result = maybe_cast_to_extension_array (cls , result , dtype = dtype )
416
+
417
+ elif (numeric_only and is_numeric_dtype (dtype )) or not numeric_only :
418
+ result = maybe_downcast_to_dtype (result , dtype )
431
419
432
420
return result
433
421
@@ -549,17 +537,23 @@ def maybe_upcast_putmask(result: np.ndarray, mask: np.ndarray) -> np.ndarray:
549
537
new_dtype = ensure_dtype_can_hold_na (result .dtype )
550
538
551
539
if new_dtype != result .dtype :
552
- # error: Argument 1 to "astype" of "_ArrayOrScalarCommon" has incompatible
553
- # type "Union[dtype[Any], ExtensionDtype]"; expected "Union[dtype[Any],
554
- # None, type, _SupportsDType, str, Union[Tuple[Any, int], Tuple[Any,
555
- # Union[int, Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]"
556
- result = result .astype (new_dtype , copy = True ) # type: ignore[arg-type]
540
+ result = result .astype (new_dtype , copy = True )
557
541
558
542
np .place (result , mask , np .nan )
559
543
560
544
return result
561
545
562
546
547
+ @overload
548
+ def ensure_dtype_can_hold_na (dtype : np .dtype ) -> np .dtype :
549
+ ...
550
+
551
+
552
+ @overload
553
+ def ensure_dtype_can_hold_na (dtype : ExtensionDtype ) -> ExtensionDtype :
554
+ ...
555
+
556
+
563
557
def ensure_dtype_can_hold_na (dtype : DtypeObj ) -> DtypeObj :
564
558
"""
565
559
If we have a dtype that cannot hold NA values, find the best match that can.
@@ -636,9 +630,7 @@ def _maybe_promote(dtype: np.dtype, fill_value=np.nan):
636
630
637
631
kinds = ["i" , "u" , "f" , "c" , "m" , "M" ]
638
632
if is_valid_na_for_dtype (fill_value , dtype ) and dtype .kind in kinds :
639
- # error: Incompatible types in assignment (expression has type
640
- # "Union[dtype[Any], ExtensionDtype]", variable has type "dtype[Any]")
641
- dtype = ensure_dtype_can_hold_na (dtype ) # type: ignore[assignment]
633
+ dtype = ensure_dtype_can_hold_na (dtype )
642
634
fv = na_value_for_dtype (dtype )
643
635
return dtype , fv
644
636
@@ -1471,7 +1463,7 @@ def soft_convert_objects(
1471
1463
1472
1464
1473
1465
def convert_dtypes (
1474
- input_array : AnyArrayLike ,
1466
+ input_array : ArrayLike ,
1475
1467
convert_string : bool = True ,
1476
1468
convert_integer : bool = True ,
1477
1469
convert_boolean : bool = True ,
@@ -1483,7 +1475,7 @@ def convert_dtypes(
1483
1475
1484
1476
Parameters
1485
1477
----------
1486
- input_array : ExtensionArray, Index, Series or np.ndarray
1478
+ input_array : ExtensionArray or np.ndarray
1487
1479
convert_string : bool, default True
1488
1480
Whether object dtypes should be converted to ``StringDtype()``.
1489
1481
convert_integer : bool, default True
@@ -1707,15 +1699,10 @@ def maybe_cast_to_datetime(
1707
1699
# GH 25843: Remove tz information since the dtype
1708
1700
# didn't specify one
1709
1701
1710
- # error: Item "ndarray" of "Union[ndarray, DatetimeArray]"
1711
- # has no attribute "tz"
1712
- if dta .tz is not None : # type: ignore[union-attr]
1702
+ if dta .tz is not None :
1713
1703
# equiv: dta.view(dtype)
1714
1704
# Note: NOT equivalent to dta.astype(dtype)
1715
-
1716
- # error: Item "ndarray" of "Union[ndarray,
1717
- # DatetimeArray]" has no attribute "tz_localize"
1718
- dta = dta .tz_localize (None ) # type: ignore[union-attr]
1705
+ dta = dta .tz_localize (None )
1719
1706
value = dta
1720
1707
elif is_datetime64tz :
1721
1708
dtype = cast (DatetimeTZDtype , dtype )
@@ -1725,38 +1712,19 @@ def maybe_cast_to_datetime(
1725
1712
# be localized to the timezone.
1726
1713
is_dt_string = is_string_dtype (value .dtype )
1727
1714
dta = sequence_to_datetimes (value , allow_object = False )
1728
- # error: Item "ndarray" of "Union[ndarray, DatetimeArray]"
1729
- # has no attribute "tz"
1730
- if dta .tz is not None : # type: ignore[union-attr]
1731
- # error: Argument 1 to "astype" of
1732
- # "_ArrayOrScalarCommon" has incompatible type
1733
- # "Union[dtype[Any], ExtensionDtype, None]"; expected
1734
- # "Union[dtype[Any], None, type, _SupportsDType, str,
1735
- # Union[Tuple[Any, int], Tuple[Any, Union[int,
1736
- # Sequence[int]]], List[Any], _DTypeDict, Tuple[Any,
1737
- # Any]]]"
1738
- value = dta .astype (
1739
- dtype , copy = False # type: ignore[arg-type]
1740
- )
1715
+ if dta .tz is not None :
1716
+ value = dta .astype (dtype , copy = False )
1741
1717
elif is_dt_string :
1742
1718
# Strings here are naive, so directly localize
1743
1719
# equiv: dta.astype(dtype) # though deprecated
1744
1720
1745
- # error: Item "ndarray" of "Union[ndarray,
1746
- # DatetimeArray]" has no attribute "tz_localize"
1747
- value = dta .tz_localize ( # type: ignore[union-attr]
1748
- dtype .tz
1749
- )
1721
+ value = dta .tz_localize (dtype .tz )
1750
1722
else :
1751
1723
# Numeric values are UTC at this point,
1752
1724
# so localize and convert
1753
1725
# equiv: Series(dta).astype(dtype) # though deprecated
1754
1726
1755
- # error: Item "ndarray" of "Union[ndarray,
1756
- # DatetimeArray]" has no attribute "tz_localize"
1757
- value = dta .tz_localize ( # type: ignore[union-attr]
1758
- "UTC"
1759
- ).tz_convert (dtype .tz )
1727
+ value = dta .tz_localize ("UTC" ).tz_convert (dtype .tz )
1760
1728
elif is_timedelta64 :
1761
1729
# if successful, we get a ndarray[td64ns]
1762
1730
value , _ = sequence_to_td64ns (value )
@@ -1789,14 +1757,12 @@ def maybe_cast_to_datetime(
1789
1757
elif value .dtype == object :
1790
1758
value = maybe_infer_to_datetimelike (value )
1791
1759
1792
- elif not isinstance (value , ABCExtensionArray ):
1760
+ elif isinstance (value , list ):
1793
1761
# only do this if we have an array and the dtype of the array is not
1794
1762
# setup already we are not an integer/object, so don't bother with this
1795
1763
# conversion
1796
1764
1797
- # error: Argument 1 to "maybe_infer_to_datetimelike" has incompatible type
1798
- # "Union[ExtensionArray, List[Any]]"; expected "Union[ndarray, List[Any]]"
1799
- value = maybe_infer_to_datetimelike (value ) # type: ignore[arg-type]
1765
+ value = maybe_infer_to_datetimelike (value )
1800
1766
1801
1767
return value
1802
1768
@@ -1974,10 +1940,8 @@ def construct_1d_arraylike_from_scalar(
1974
1940
except OutOfBoundsDatetime :
1975
1941
dtype = np .dtype (object )
1976
1942
1977
- if is_extension_array_dtype (dtype ):
1978
- # error: Item "dtype" of "Union[dtype, ExtensionDtype]" has no
1979
- # attribute "construct_array_type"
1980
- cls = dtype .construct_array_type () # type: ignore[union-attr]
1943
+ if isinstance (dtype , ExtensionDtype ):
1944
+ cls = dtype .construct_array_type ()
1981
1945
subarr = cls ._from_sequence ([value ] * length , dtype = dtype )
1982
1946
1983
1947
else :
@@ -1994,11 +1958,7 @@ def construct_1d_arraylike_from_scalar(
1994
1958
elif dtype .kind in ["M" , "m" ]:
1995
1959
value = maybe_unbox_datetimelike (value , dtype )
1996
1960
1997
- # error: Argument "dtype" to "empty" has incompatible type
1998
- # "Union[dtype, ExtensionDtype]"; expected "Union[dtype, None, type,
1999
- # _SupportsDtype, str, Tuple[Any, int], Tuple[Any, Union[int,
2000
- # Sequence[int]]], List[Any], _DtypeDict, Tuple[Any, Any]]"
2001
- subarr = np .empty (length , dtype = dtype ) # type: ignore[arg-type]
1961
+ subarr = np .empty (length , dtype = dtype )
2002
1962
subarr .fill (value )
2003
1963
2004
1964
return subarr
0 commit comments