Skip to content

Commit bb8abd7

Browse files
committed
Merge remote branch 'chang/frame-dtype'
* chang/frame-dtype: Get rid of newline. Triggering Travis CLN: put new kwd at the end BUG: dtype=object should stop conversion from object in frame constructor #2255
2 parents 9edd478 + b5ea387 commit bb8abd7

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

pandas/core/frame.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
399399
index = _get_names_from_index(data)
400400

401401
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)
404403
columns = _ensure_index(columns)
405404

406405
if index is None:
@@ -5209,38 +5208,43 @@ def _rec_to_dict(arr):
52095208
return columns, sdict
52105209

52115210

5212-
def _to_arrays(data, columns, coerce_float=False):
5211+
def _to_arrays(data, columns, coerce_float=False, dtype=None):
52135212
"""
52145213
Return list of arrays, columns
52155214
"""
52165215

52175216
if len(data) == 0:
52185217
return [], columns if columns is not None else []
52195218
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)
52215221
elif isinstance(data[0], dict):
52225222
return _list_of_dict_to_arrays(data, columns,
5223-
coerce_float=coerce_float)
5223+
coerce_float=coerce_float,
5224+
dtype=dtype)
52245225
elif isinstance(data[0], Series):
52255226
return _list_of_series_to_arrays(data, columns,
5226-
coerce_float=coerce_float)
5227+
coerce_float=coerce_float,
5228+
dtype=dtype)
52275229
else:
52285230
# last ditch effort
52295231
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)
52315235

52325236

5233-
def _list_to_arrays(data, columns, coerce_float=False):
5237+
def _list_to_arrays(data, columns, coerce_float=False, dtype=None):
52345238
if len(data) > 0 and isinstance(data[0], tuple):
52355239
content = list(lib.to_object_array_tuples(data).T)
52365240
else:
52375241
# list of lists
52385242
content = list(lib.to_object_array(data).T)
5239-
return _convert_object_array(content, columns,
5243+
return _convert_object_array(content, columns, dtype=dtype,
52405244
coerce_float=coerce_float)
52415245

52425246

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):
52445248
from pandas.core.index import _get_combined_index
52455249

52465250
if columns is None:
@@ -5261,13 +5265,13 @@ def _list_of_series_to_arrays(data, columns, coerce_float=False):
52615265

52625266
if values.dtype == np.object_:
52635267
content = list(values.T)
5264-
return _convert_object_array(content, columns,
5268+
return _convert_object_array(content, columns, dtype=dtype,
52655269
coerce_float=coerce_float)
52665270
else:
52675271
return values.T, columns
52685272

52695273

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):
52715275
if columns is None:
52725276
gen = (x.keys() for x in data)
52735277
columns = lib.fast_unique_multiple_list_gen(gen)
@@ -5278,11 +5282,11 @@ def _list_of_dict_to_arrays(data, columns, coerce_float=False):
52785282
for d in data]
52795283

52805284
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,
52825286
coerce_float=coerce_float)
52835287

52845288

5285-
def _convert_object_array(content, columns, coerce_float=False):
5289+
def _convert_object_array(content, columns, coerce_float=False, dtype=None):
52865290
if columns is None:
52875291
columns = _default_index(len(content))
52885292
else:
@@ -5291,6 +5295,7 @@ def _convert_object_array(content, columns, coerce_float=False):
52915295
'columns' % (len(columns), len(content)))
52925296

52935297
arrays = [lib.maybe_convert_objects(arr, try_float=coerce_float)
5298+
if dtype != object and dtype != np.object else arr
52945299
for arr in content]
52955300

52965301
return arrays, columns

pandas/tests/test_frame.py

+6
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,12 @@ def test_constructor_dtype_nocast_view(self):
17301730
should_be_view[0][0] = 97
17311731
self.assertEqual(df.values[0, 0], 97)
17321732

1733+
def test_constructor_dtype_list_data(self):
1734+
df = DataFrame([[1, '2'],
1735+
[None, 'a']], dtype=object)
1736+
self.assert_(df.ix[1, 0] is None)
1737+
self.assert_(df.ix[0, 1] == '2')
1738+
17331739
def test_constructor_rec(self):
17341740
rec = self.frame.to_records(index=False)
17351741

0 commit comments

Comments
 (0)