44
55from __future__ import annotations
66
7- from datetime import (
8- datetime ,
9- timedelta ,
10- )
7+ import datetime as dt
118import functools
129from typing import (
1310 TYPE_CHECKING ,
7370 is_string_dtype ,
7471 is_timedelta64_dtype ,
7572 is_unsigned_integer_dtype ,
76- pandas_dtype ,
73+ pandas_dtype as pandas_dtype_func ,
7774)
7875from pandas .core .dtypes .dtypes import (
7976 CategoricalDtype ,
@@ -170,9 +167,9 @@ def maybe_box_datetimelike(value: Scalar, dtype: Dtype | None = None) -> Scalar:
170167 """
171168 if dtype == _dtype_obj :
172169 pass
173- elif isinstance (value , (np .datetime64 , datetime )):
170+ elif isinstance (value , (np .datetime64 , dt . datetime )):
174171 value = Timestamp (value )
175- elif isinstance (value , (np .timedelta64 , timedelta )):
172+ elif isinstance (value , (np .timedelta64 , dt . timedelta )):
176173 value = Timedelta (value )
177174
178175 return value
@@ -761,7 +758,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> tuple[DtypeObj,
761758
762759 dtype = _dtype_obj
763760
764- elif isinstance (val , (np .datetime64 , datetime )):
761+ elif isinstance (val , (np .datetime64 , dt . datetime )):
765762 try :
766763 val = Timestamp (val )
767764 except OutOfBoundsDatetime :
@@ -781,7 +778,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> tuple[DtypeObj,
781778 # return datetimetz as object
782779 return _dtype_obj , val
783780
784- elif isinstance (val , (np .timedelta64 , timedelta )):
781+ elif isinstance (val , (np .timedelta64 , dt . timedelta )):
785782 try :
786783 val = Timedelta (val )
787784 except (OutOfBoundsTimedelta , OverflowError ):
@@ -1096,10 +1093,10 @@ def convert_dtypes(
10961093 if not convert_string or inferred_dtype == "bytes" :
10971094 return input_array .dtype
10981095 else :
1099- return pandas_dtype ("string" )
1096+ return pandas_dtype_func ("string" )
11001097
11011098 if convert_integer :
1102- target_int_dtype = pandas_dtype ("Int64" )
1099+ target_int_dtype = pandas_dtype_func ("Int64" )
11031100
11041101 if is_integer_dtype (input_array .dtype ):
11051102 from pandas .core .arrays .integer import INT_STR_TO_DTYPE
@@ -1128,15 +1125,15 @@ def convert_dtypes(
11281125 from pandas .core .arrays .floating import FLOAT_STR_TO_DTYPE
11291126
11301127 inferred_float_dtype : DtypeObj = FLOAT_STR_TO_DTYPE .get (
1131- input_array .dtype .name , pandas_dtype ("Float64" )
1128+ input_array .dtype .name , pandas_dtype_func ("Float64" )
11321129 )
11331130 # if we could also convert to integer, check if all floats
11341131 # are actually integers
11351132 if convert_integer :
11361133 # TODO: de-dup with maybe_cast_to_integer_array?
11371134 arr = input_array [notna (input_array )]
11381135 if (arr .astype (int ) == arr ).all ():
1139- inferred_dtype = pandas_dtype ("Int64" )
1136+ inferred_dtype = pandas_dtype_func ("Int64" )
11401137 else :
11411138 inferred_dtype = inferred_float_dtype
11421139 else :
@@ -1146,13 +1143,13 @@ def convert_dtypes(
11461143 and is_object_dtype (input_array .dtype )
11471144 and inferred_dtype == "mixed-integer-float"
11481145 ):
1149- inferred_dtype = pandas_dtype ("Float64" )
1146+ inferred_dtype = pandas_dtype_func ("Float64" )
11501147
11511148 if convert_boolean :
11521149 if is_bool_dtype (input_array .dtype ):
1153- inferred_dtype = pandas_dtype ("boolean" )
1150+ inferred_dtype = pandas_dtype_func ("boolean" )
11541151 elif isinstance (inferred_dtype , str ) and inferred_dtype == "boolean" :
1155- inferred_dtype = pandas_dtype ("boolean" )
1152+ inferred_dtype = pandas_dtype_func ("boolean" )
11561153
11571154 if isinstance (inferred_dtype , str ):
11581155 # If we couldn't do anything else, then we retain the dtype
@@ -1578,7 +1575,7 @@ def construct_1d_arraylike_from_scalar(
15781575def _maybe_box_and_unbox_datetimelike (value : Scalar , dtype : DtypeObj ):
15791576 # Caller is responsible for checking dtype.kind in ["m", "M"]
15801577
1581- if isinstance (value , datetime ):
1578+ if isinstance (value , dt . datetime ):
15821579 # we dont want to box dt64, in particular datetime64("NaT")
15831580 value = maybe_box_datetimelike (value , dtype )
15841581
0 commit comments