@@ -1173,15 +1173,15 @@ cdef class Seen:
11731173 or self .nat_)
11741174
11751175
1176- cdef object _try_infer_map(object v ):
1176+ cdef object _try_infer_map(object dtype ):
11771177 """
11781178 If its in our map, just return the dtype.
11791179 """
11801180 cdef:
11811181 object val
11821182 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)
11851185 if val in _TYPE_MAP:
11861186 return _TYPE_MAP[val]
11871187 return None
@@ -1294,44 +1294,49 @@ def infer_dtype(value: object, skipna: bool = True) -> str:
12941294
12951295 if util.is_array(value ):
12961296 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" ):
12981301 # this will handle ndarray-like
12991302 # 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)
13061306 if value is not None :
13071307 return value
13081308
1309- # its ndarray like but we can't handle
1309+ # its ndarray- like but we can't handle
13101310 raise ValueError (f" cannot infer type for {type(value)}" )
13111311
1312+ # Unwrap Series/Index
1313+ values = np.asarray(value)
1314+
13121315 else :
13131316 if not isinstance (value, list ):
13141317 value = list (value)
1318+
13151319 from pandas.core.dtypes.cast import (
13161320 construct_1d_object_array_from_listlike)
13171321 values = construct_1d_object_array_from_listlike(value)
13181322
13191323 # 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" )
13211326
1322- val = _try_infer_map(values)
1327+ val = _try_infer_map(values.dtype )
13231328 if val is not None :
13241329 return val
13251330
13261331 if values.dtype != np.object_:
1327- values = values.astype(' O ' )
1332+ values = values.astype(" O " )
13281333
13291334 if skipna:
13301335 values = values[~ isnaobj(values)]
13311336
13321337 n = len (values)
13331338 if n == 0 :
1334- return ' empty'
1339+ return " empty"
13351340
13361341 # try to use a valid value
13371342 for i in range (n):
0 commit comments