70
70
is_timedelta64_dtype ,
71
71
is_timedelta64_ns_dtype ,
72
72
is_unsigned_integer_dtype ,
73
- pandas_dtype ,
74
73
)
75
74
from pandas .core .dtypes .dtypes import (
76
75
DatetimeTZDtype ,
80
79
)
81
80
from pandas .core .dtypes .generic import (
82
81
ABCDataFrame ,
83
- ABCDatetimeArray ,
84
- ABCDatetimeIndex ,
85
82
ABCExtensionArray ,
86
- ABCPeriodArray ,
87
- ABCPeriodIndex ,
83
+ ABCIndex ,
88
84
ABCSeries ,
89
85
)
90
86
from pandas .core .dtypes .inference import is_list_like
@@ -965,7 +961,7 @@ def astype_nansafe(
965
961
Parameters
966
962
----------
967
963
arr : ndarray
968
- dtype : np.dtype
964
+ dtype : np.dtype or ExtensionDtype
969
965
copy : bool, default True
970
966
If False, a view will be attempted but may fail, if
971
967
e.g. the item sizes don't align.
@@ -978,11 +974,11 @@ def astype_nansafe(
978
974
The dtype was a datetime64/timedelta64 dtype, but it had no unit.
979
975
"""
980
976
# dispatch on extension dtype if needed
981
- if is_extension_array_dtype (dtype ):
977
+ if isinstance (dtype , ExtensionDtype ):
982
978
return dtype .construct_array_type ()._from_sequence (arr , dtype = dtype , copy = copy )
983
979
984
- if not isinstance (dtype , np .dtype ):
985
- dtype = pandas_dtype ( dtype )
980
+ elif not isinstance (dtype , np .dtype ):
981
+ raise ValueError ( " dtype must be np. dtype or ExtensionDtype" )
986
982
987
983
if issubclass (dtype .type , str ):
988
984
return lib .ensure_string_array (
@@ -1252,11 +1248,9 @@ def maybe_infer_to_datetimelike(
1252
1248
leave inferred dtype 'date' alone
1253
1249
1254
1250
"""
1255
- # TODO: why not timedelta?
1256
- if isinstance (
1257
- value , (ABCDatetimeIndex , ABCPeriodIndex , ABCDatetimeArray , ABCPeriodArray )
1258
- ):
1259
- return value
1251
+ if isinstance (value , (ABCIndex , ABCExtensionArray )):
1252
+ if not is_object_dtype (value .dtype ):
1253
+ raise ValueError ("array-like value must be object-dtype" )
1260
1254
1261
1255
v = value
1262
1256
@@ -1431,7 +1425,7 @@ def maybe_cast_to_datetime(value, dtype: Optional[DtypeObj]):
1431
1425
value = to_timedelta (value , errors = "raise" )._values
1432
1426
except OutOfBoundsDatetime :
1433
1427
raise
1434
- except (AttributeError , ValueError , TypeError ):
1428
+ except (ValueError , TypeError ):
1435
1429
pass
1436
1430
1437
1431
# coerce datetimelike to object
0 commit comments