@@ -147,9 +147,18 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
147
147
mgr = mgr .cast (dtype )
148
148
elif isinstance (data , dict ):
149
149
mgr = self ._init_dict (data , index , columns , dtype = dtype )
150
- elif isinstance (data , (np .ndarray , list )):
151
- mgr = self ._init_matrix (data , index , columns , dtype = dtype ,
152
- copy = copy )
150
+ elif isinstance (data , np .ndarray ):
151
+ if data .dtype .names :
152
+ data_columns , data = _rec_to_dict (data )
153
+ if columns is None :
154
+ columns = data_columns
155
+ mgr = self ._init_dict (data , index , columns , dtype = dtype )
156
+ else :
157
+ mgr = self ._init_ndarray (data , index , columns , dtype = dtype ,
158
+ copy = copy )
159
+ elif isinstance (data , list ):
160
+ mgr = self ._init_ndarray (data , index , columns , dtype = dtype ,
161
+ copy = copy )
153
162
else :
154
163
raise PandasError ('DataFrame constructor not properly called!' )
155
164
@@ -183,8 +192,8 @@ def _init_dict(self, data, index, columns, dtype=None):
183
192
mgr = BlockManager (blocks , [columns , index ])
184
193
return mgr .consolidate ()
185
194
186
- def _init_matrix (self , values , index , columns , dtype = None ,
187
- copy = False ):
195
+ def _init_ndarray (self , values , index , columns , dtype = None ,
196
+ copy = False ):
188
197
values = _prep_ndarray (values , copy = copy )
189
198
190
199
if dtype is not None :
@@ -347,16 +356,13 @@ def from_records(cls, data, indexField=None):
347
356
-------
348
357
DataFrame
349
358
"""
350
- # Dtype when you have records
351
- if not issubclass (data .dtype .type , np .void ):
359
+ if not data .dtype .names :
352
360
raise Exception ('Input was not a structured array!' )
353
361
354
- columns = data .dtype .names
355
- sdict = dict ((k , data [k ]) for k in columns )
356
-
362
+ columns , sdict = _rec_to_dict (data )
357
363
if indexField is not None :
358
364
index = sdict .pop (indexField )
359
- columns = [ c for c in columns if c != indexField ]
365
+ columns . remove ( indexField )
360
366
else :
361
367
index = np .arange (len (data ))
362
368
@@ -2484,6 +2490,12 @@ def _prep_ndarray(values, copy=True):
2484
2490
2485
2491
return values
2486
2492
2493
+
2494
+ def _rec_to_dict (arr ):
2495
+ columns = list (arr .dtype .names )
2496
+ sdict = dict ((k , arr [k ]) for k in columns )
2497
+ return columns , sdict
2498
+
2487
2499
def _homogenize_series (data , index , dtype = None ):
2488
2500
homogenized = {}
2489
2501
@@ -2507,9 +2519,9 @@ def _homogenize_series(data, index, dtype=None):
2507
2519
2508
2520
# only *attempt* to cast to dtype
2509
2521
try :
2510
- v = Series (v , dtype = dtype , index = index )
2522
+ v = np . asarray (v , dtype = dtype )
2511
2523
except Exception :
2512
- v = Series ( v , index = index )
2524
+ v = np . asarray ( v )
2513
2525
2514
2526
homogenized [k ] = v
2515
2527
0 commit comments