4
4
5
5
from __future__ import annotations
6
6
7
- from datetime import (
8
- datetime ,
9
- timedelta ,
10
- )
7
+ import datetime as dt
11
8
import functools
12
9
from typing import (
13
10
TYPE_CHECKING ,
73
70
is_string_dtype ,
74
71
is_timedelta64_dtype ,
75
72
is_unsigned_integer_dtype ,
76
- pandas_dtype ,
73
+ pandas_dtype as pandas_dtype_func ,
77
74
)
78
75
from pandas .core .dtypes .dtypes import (
79
76
CategoricalDtype ,
@@ -170,9 +167,9 @@ def maybe_box_datetimelike(value: Scalar, dtype: Dtype | None = None) -> Scalar:
170
167
"""
171
168
if dtype == _dtype_obj :
172
169
pass
173
- elif isinstance (value , (np .datetime64 , datetime )):
170
+ elif isinstance (value , (np .datetime64 , dt . datetime )):
174
171
value = Timestamp (value )
175
- elif isinstance (value , (np .timedelta64 , timedelta )):
172
+ elif isinstance (value , (np .timedelta64 , dt . timedelta )):
176
173
value = Timedelta (value )
177
174
178
175
return value
@@ -761,7 +758,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> tuple[DtypeObj,
761
758
762
759
dtype = _dtype_obj
763
760
764
- elif isinstance (val , (np .datetime64 , datetime )):
761
+ elif isinstance (val , (np .datetime64 , dt . datetime )):
765
762
try :
766
763
val = Timestamp (val )
767
764
except OutOfBoundsDatetime :
@@ -781,7 +778,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> tuple[DtypeObj,
781
778
# return datetimetz as object
782
779
return _dtype_obj , val
783
780
784
- elif isinstance (val , (np .timedelta64 , timedelta )):
781
+ elif isinstance (val , (np .timedelta64 , dt . timedelta )):
785
782
try :
786
783
val = Timedelta (val )
787
784
except (OutOfBoundsTimedelta , OverflowError ):
@@ -1096,10 +1093,10 @@ def convert_dtypes(
1096
1093
if not convert_string or inferred_dtype == "bytes" :
1097
1094
return input_array .dtype
1098
1095
else :
1099
- return pandas_dtype ("string" )
1096
+ return pandas_dtype_func ("string" )
1100
1097
1101
1098
if convert_integer :
1102
- target_int_dtype = pandas_dtype ("Int64" )
1099
+ target_int_dtype = pandas_dtype_func ("Int64" )
1103
1100
1104
1101
if is_integer_dtype (input_array .dtype ):
1105
1102
from pandas .core .arrays .integer import INT_STR_TO_DTYPE
@@ -1128,15 +1125,15 @@ def convert_dtypes(
1128
1125
from pandas .core .arrays .floating import FLOAT_STR_TO_DTYPE
1129
1126
1130
1127
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" )
1132
1129
)
1133
1130
# if we could also convert to integer, check if all floats
1134
1131
# are actually integers
1135
1132
if convert_integer :
1136
1133
# TODO: de-dup with maybe_cast_to_integer_array?
1137
1134
arr = input_array [notna (input_array )]
1138
1135
if (arr .astype (int ) == arr ).all ():
1139
- inferred_dtype = pandas_dtype ("Int64" )
1136
+ inferred_dtype = pandas_dtype_func ("Int64" )
1140
1137
else :
1141
1138
inferred_dtype = inferred_float_dtype
1142
1139
else :
@@ -1146,13 +1143,13 @@ def convert_dtypes(
1146
1143
and is_object_dtype (input_array .dtype )
1147
1144
and inferred_dtype == "mixed-integer-float"
1148
1145
):
1149
- inferred_dtype = pandas_dtype ("Float64" )
1146
+ inferred_dtype = pandas_dtype_func ("Float64" )
1150
1147
1151
1148
if convert_boolean :
1152
1149
if is_bool_dtype (input_array .dtype ):
1153
- inferred_dtype = pandas_dtype ("boolean" )
1150
+ inferred_dtype = pandas_dtype_func ("boolean" )
1154
1151
elif isinstance (inferred_dtype , str ) and inferred_dtype == "boolean" :
1155
- inferred_dtype = pandas_dtype ("boolean" )
1152
+ inferred_dtype = pandas_dtype_func ("boolean" )
1156
1153
1157
1154
if isinstance (inferred_dtype , str ):
1158
1155
# If we couldn't do anything else, then we retain the dtype
@@ -1578,7 +1575,7 @@ def construct_1d_arraylike_from_scalar(
1578
1575
def _maybe_box_and_unbox_datetimelike (value : Scalar , dtype : DtypeObj ):
1579
1576
# Caller is responsible for checking dtype.kind in ["m", "M"]
1580
1577
1581
- if isinstance (value , datetime ):
1578
+ if isinstance (value , dt . datetime ):
1582
1579
# we dont want to box dt64, in particular datetime64("NaT")
1583
1580
value = maybe_box_datetimelike (value , dtype )
1584
1581
0 commit comments