@@ -1173,15 +1173,15 @@ cdef class Seen:
1173
1173
or self .nat_)
1174
1174
1175
1175
1176
- cdef object _try_infer_map(object v ):
1176
+ cdef object _try_infer_map(object dtype ):
1177
1177
"""
1178
1178
If its in our map, just return the dtype.
1179
1179
"""
1180
1180
cdef:
1181
1181
object val
1182
1182
str attr
1183
- for attr in [' name' , ' kind' , ' base' ]:
1184
- val = getattr (v. dtype, attr)
1183
+ for attr in [" name" , " kind" , " base" ]:
1184
+ val = getattr (dtype, attr)
1185
1185
if val in _TYPE_MAP:
1186
1186
return _TYPE_MAP[val]
1187
1187
return None
@@ -1294,44 +1294,49 @@ def infer_dtype(value: object, skipna: bool = True) -> str:
1294
1294
1295
1295
if util.is_array(value ):
1296
1296
values = value
1297
- elif hasattr (value, ' dtype' ):
1297
+ elif hasattr (value, " inferred_type" ) and skipna is False :
1298
+ # Index, use the cached attribute if possible, populate the cache otherwise
1299
+ return value.inferred_type
1300
+ elif hasattr (value, " dtype" ):
1298
1301
# this will handle ndarray-like
1299
1302
# e.g. categoricals
1300
- try :
1301
- values = getattr (value, ' _values' , getattr (value, ' values' , value))
1302
- except TypeError :
1303
- # This gets hit if we have an EA, since cython expects `values`
1304
- # to be an ndarray
1305
- value = _try_infer_map(value)
1303
+ dtype = value.dtype
1304
+ if not isinstance (dtype, np.dtype):
1305
+ value = _try_infer_map(value.dtype)
1306
1306
if value is not None :
1307
1307
return value
1308
1308
1309
- # its ndarray like but we can't handle
1309
+ # its ndarray- like but we can't handle
1310
1310
raise ValueError (f" cannot infer type for {type(value)}" )
1311
1311
1312
+ # Unwrap Series/Index
1313
+ values = np.asarray(value)
1314
+
1312
1315
else :
1313
1316
if not isinstance (value, list ):
1314
1317
value = list (value)
1318
+
1315
1319
from pandas.core.dtypes.cast import (
1316
1320
construct_1d_object_array_from_listlike)
1317
1321
values = construct_1d_object_array_from_listlike(value)
1318
1322
1319
1323
# make contiguous
1320
- values = values.ravel()
1324
+ # for f-contiguous array 1000 x 1000, passing order="K" gives 5000x speedup
1325
+ values = values.ravel(order = " K" )
1321
1326
1322
- val = _try_infer_map(values)
1327
+ val = _try_infer_map(values.dtype )
1323
1328
if val is not None :
1324
1329
return val
1325
1330
1326
1331
if values.dtype != np.object_:
1327
- values = values.astype(' O ' )
1332
+ values = values.astype(" O " )
1328
1333
1329
1334
if skipna:
1330
1335
values = values[~ isnaobj(values)]
1331
1336
1332
1337
n = len (values)
1333
1338
if n == 0 :
1334
- return ' empty'
1339
+ return " empty"
1335
1340
1336
1341
# try to use a valid value
1337
1342
for i in range (n):
0 commit comments