@@ -78,6 +78,16 @@ def _ensure_data(values, dtype=None):
78
78
elif is_float_dtype (values ) or is_float_dtype (dtype ):
79
79
return _ensure_float64 (values ), 'float64' , 'float64'
80
80
elif is_object_dtype (values ) and dtype is None :
81
+
82
+ # if we can infer a numeric then do this
83
+ inferred = lib .infer_dtype (values )
84
+ if inferred in ['integer' ]:
85
+ return _ensure_int64 (values ), 'int64' , 'int64'
86
+ elif inferred in ['floating' ]:
87
+ return _ensure_float64 (values ), 'float64' , 'float64'
88
+ elif inferred in ['boolean' ]:
89
+ return np .asarray (values ).astype ('uint64' ), 'bool' , 'uint64'
90
+
81
91
return _ensure_object (np .asarray (values )), 'object' , 'object'
82
92
elif is_complex_dtype (values ) or is_complex_dtype (dtype ):
83
93
@@ -87,7 +97,7 @@ def _ensure_data(values, dtype=None):
87
97
values = _ensure_float64 (values )
88
98
return values , 'float64' , 'float64'
89
99
90
- except (TypeError , ValueError ):
100
+ except (TypeError , ValueError , OverflowError ):
91
101
# if we are trying to coerce to a dtype
92
102
# and it is incompat this will fall thru to here
93
103
return _ensure_object (values ), 'object' , 'object'
@@ -150,7 +160,13 @@ def _reconstruct_data(values, dtype, original):
150
160
elif is_datetime64tz_dtype (dtype ) or is_period_dtype (dtype ):
151
161
values = Index (original )._shallow_copy (values , name = None )
152
162
elif dtype is not None :
153
- values = values .astype (dtype )
163
+
164
+ # don't cast to object if we are numeric
165
+ if is_object_dtype (dtype ):
166
+ if not is_numeric_dtype (values ):
167
+ values = values .astype (dtype )
168
+ else :
169
+ values = values .astype (dtype )
154
170
155
171
return values
156
172
@@ -161,7 +177,7 @@ def _ensure_arraylike(values):
161
177
"""
162
178
if not isinstance (values , (np .ndarray , ABCCategorical ,
163
179
ABCIndexClass , ABCSeries )):
164
- values = np .array (values )
180
+ values = np .array (values , dtype = object )
165
181
return values
166
182
167
183
0 commit comments