@@ -399,8 +399,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
399
399
index = _get_names_from_index (data )
400
400
401
401
if isinstance (data [0 ], (list , tuple , dict , Series )):
402
- arrays , columns = _to_arrays (data , columns )
403
-
402
+ arrays , columns = _to_arrays (data , columns , dtype = dtype )
404
403
columns = _ensure_index (columns )
405
404
406
405
if index is None :
@@ -5209,38 +5208,43 @@ def _rec_to_dict(arr):
5209
5208
return columns , sdict
5210
5209
5211
5210
5212
- def _to_arrays (data , columns , coerce_float = False ):
5211
+ def _to_arrays (data , columns , coerce_float = False , dtype = None ):
5213
5212
"""
5214
5213
Return list of arrays, columns
5215
5214
"""
5216
5215
5217
5216
if len (data ) == 0 :
5218
5217
return [], columns if columns is not None else []
5219
5218
if isinstance (data [0 ], (list , tuple )):
5220
- return _list_to_arrays (data , columns , coerce_float = coerce_float )
5219
+ return _list_to_arrays (data , columns , coerce_float = coerce_float ,
5220
+ dtype = dtype )
5221
5221
elif isinstance (data [0 ], dict ):
5222
5222
return _list_of_dict_to_arrays (data , columns ,
5223
- coerce_float = coerce_float )
5223
+ coerce_float = coerce_float ,
5224
+ dtype = dtype )
5224
5225
elif isinstance (data [0 ], Series ):
5225
5226
return _list_of_series_to_arrays (data , columns ,
5226
- coerce_float = coerce_float )
5227
+ coerce_float = coerce_float ,
5228
+ dtype = dtype )
5227
5229
else :
5228
5230
# last ditch effort
5229
5231
data = map (tuple , data )
5230
- return _list_to_arrays (data , columns , coerce_float = coerce_float )
5232
+ return _list_to_arrays (data , columns ,
5233
+ coerce_float = coerce_float ,
5234
+ dtype = dtype )
5231
5235
5232
5236
5233
- def _list_to_arrays (data , columns , coerce_float = False ):
5237
+ def _list_to_arrays (data , columns , coerce_float = False , dtype = None ):
5234
5238
if len (data ) > 0 and isinstance (data [0 ], tuple ):
5235
5239
content = list (lib .to_object_array_tuples (data ).T )
5236
5240
else :
5237
5241
# list of lists
5238
5242
content = list (lib .to_object_array (data ).T )
5239
- return _convert_object_array (content , columns ,
5243
+ return _convert_object_array (content , columns , dtype = dtype ,
5240
5244
coerce_float = coerce_float )
5241
5245
5242
5246
5243
- def _list_of_series_to_arrays (data , columns , coerce_float = False ):
5247
+ def _list_of_series_to_arrays (data , columns , coerce_float = False , dtype = None ):
5244
5248
from pandas .core .index import _get_combined_index
5245
5249
5246
5250
if columns is None :
@@ -5261,13 +5265,13 @@ def _list_of_series_to_arrays(data, columns, coerce_float=False):
5261
5265
5262
5266
if values .dtype == np .object_ :
5263
5267
content = list (values .T )
5264
- return _convert_object_array (content , columns ,
5268
+ return _convert_object_array (content , columns , dtype = dtype ,
5265
5269
coerce_float = coerce_float )
5266
5270
else :
5267
5271
return values .T , columns
5268
5272
5269
5273
5270
- def _list_of_dict_to_arrays (data , columns , coerce_float = False ):
5274
+ def _list_of_dict_to_arrays (data , columns , coerce_float = False , dtype = None ):
5271
5275
if columns is None :
5272
5276
gen = (x .keys () for x in data )
5273
5277
columns = lib .fast_unique_multiple_list_gen (gen )
@@ -5278,11 +5282,11 @@ def _list_of_dict_to_arrays(data, columns, coerce_float=False):
5278
5282
for d in data ]
5279
5283
5280
5284
content = list (lib .dicts_to_array (data , list (columns )).T )
5281
- return _convert_object_array (content , columns ,
5285
+ return _convert_object_array (content , columns , dtype = dtype ,
5282
5286
coerce_float = coerce_float )
5283
5287
5284
5288
5285
- def _convert_object_array (content , columns , coerce_float = False ):
5289
+ def _convert_object_array (content , columns , coerce_float = False , dtype = None ):
5286
5290
if columns is None :
5287
5291
columns = _default_index (len (content ))
5288
5292
else :
@@ -5291,6 +5295,7 @@ def _convert_object_array(content, columns, coerce_float=False):
5291
5295
'columns' % (len (columns ), len (content )))
5292
5296
5293
5297
arrays = [lib .maybe_convert_objects (arr , try_float = coerce_float )
5298
+ if dtype != object and dtype != np .object else arr
5294
5299
for arr in content ]
5295
5300
5296
5301
return arrays , columns
0 commit comments