@@ -21,6 +21,8 @@ def is_period(object val):
21
21
return util.is_period_object(val)
22
22
23
23
_TYPE_MAP = {
24
+ ' categorical' : ' categorical' ,
25
+ ' category' : ' categorical' ,
24
26
' int8' : ' integer' ,
25
27
' int16' : ' integer' ,
26
28
' int32' : ' integer' ,
65
67
except AttributeError :
66
68
pass
67
69
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
+
68
82
def infer_dtype (object _values ):
83
+ """
84
+ we are coercing to an ndarray here
85
+ """
86
+
69
87
cdef:
70
88
Py_ssize_t i, n
71
89
object val
72
90
ndarray values
73
91
74
92
if isinstance (_values, np.ndarray):
75
93
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
+
78
108
else :
79
109
if not isinstance (_values, list ):
80
110
_values = list (_values)
81
111
values = list_to_object_array(_values)
82
112
83
113
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
91
117
92
118
if values.dtype != np.object_:
93
119
values = values.astype(' O' )
0 commit comments