@@ -21,6 +21,8 @@ def is_period(object val):
2121 return util.is_period_object(val)
2222
2323_TYPE_MAP = {
24+ ' categorical' : ' categorical' ,
25+ ' category' : ' categorical' ,
2426 ' int8' : ' integer' ,
2527 ' int16' : ' integer' ,
2628 ' int32' : ' integer' ,
6567except AttributeError :
6668 pass
6769
70+ cdef _try_infer_map(v):
71+ """ if its in our map, just return the dtype """
72+ cdef:
73+ object val_name, val_kind
74+ val_name = v.dtype.name
75+ if val_name in _TYPE_MAP:
76+ return _TYPE_MAP[val_name]
77+ val_kind = v.dtype.kind
78+ if val_kind in _TYPE_MAP:
79+ return _TYPE_MAP[val_kind]
80+ return None
81+
6882def infer_dtype (object _values ):
83+ """
84+ we are coercing to an ndarray here
85+ """
86+
6987 cdef:
7088 Py_ssize_t i, n
7189 object val
7290 ndarray values
7391
7492 if isinstance (_values, np.ndarray):
7593 values = _values
76- elif hasattr (_values,' values' ):
77- values = _values.values
94+ elif hasattr (_values,' dtype' ):
95+
96+ # this will handle ndarray-like
97+ # e.g. categoricals
98+ try :
99+ values = getattr (_values, ' values' , _values)
100+ except :
101+ val = _try_infer_map(_values)
102+ if val is not None :
103+ return val
104+
105+ # its ndarray like but we can't handle
106+ raise ValueError (" cannot infer type for {0}" .format(type (_values)))
107+
78108 else :
79109 if not isinstance (_values, list ):
80110 _values = list (_values)
81111 values = list_to_object_array(_values)
82112
83113 values = getattr (values, ' values' , values)
84-
85- val_name = values.dtype.name
86- if val_name in _TYPE_MAP:
87- return _TYPE_MAP[val_name]
88- val_kind = values.dtype.kind
89- if val_kind in _TYPE_MAP:
90- return _TYPE_MAP[val_kind]
114+ val = _try_infer_map(values)
115+ if val is not None :
116+ return val
91117
92118 if values.dtype != np.object_:
93119 values = values.astype(' O' )
0 commit comments