26
26
from pandas .core .common import (isnull , notnull , PandasError , _try_sort ,
27
27
_default_index , _maybe_upcast , _is_sequence ,
28
28
_infer_dtype_from_scalar , _values_from_object ,
29
- is_list_like , _get_dtype , _maybe_box_datetimelike )
29
+ is_list_like , _get_dtype , _maybe_box_datetimelike ,
30
+ is_categorical_dtype )
30
31
from pandas .core .generic import NDFrame , _shared_docs
31
32
from pandas .core .index import Index , MultiIndex , _ensure_index
32
33
from pandas .core .indexing import (_maybe_droplevels ,
@@ -332,6 +333,8 @@ def _init_dict(self, data, index, columns, dtype=None):
332
333
333
334
def _init_ndarray (self , values , index , columns , dtype = None ,
334
335
copy = False ):
336
+ # input must be a ndarray, list, Series, index
337
+
335
338
if isinstance (values , Series ):
336
339
if columns is None :
337
340
if values .name is not None :
@@ -345,9 +348,41 @@ def _init_ndarray(self, values, index, columns, dtype=None,
345
348
if not len (values ) and columns is not None and len (columns ):
346
349
values = np .empty ((0 , 1 ), dtype = object )
347
350
351
+ # helper to create the axes as indexes
352
+ def _get_axes (N , K , index = index , columns = columns ):
353
+ # return axes or defaults
354
+
355
+ if index is None :
356
+ index = _default_index (N )
357
+ else :
358
+ index = _ensure_index (index )
359
+
360
+ if columns is None :
361
+ columns = _default_index (K )
362
+ else :
363
+ columns = _ensure_index (columns )
364
+ return index , columns
365
+
366
+ # we could have a categorical type passed or coerced to 'category'
367
+ # recast this to an _arrays_to_mgr
368
+ if is_categorical_dtype (getattr (values ,'dtype' ,None )) or is_categorical_dtype (dtype ):
369
+
370
+ if not hasattr (values ,'dtype' ):
371
+ values = _prep_ndarray (values , copy = copy )
372
+ values = values .ravel ()
373
+ elif copy :
374
+ values = values .copy ()
375
+
376
+ index , columns = _get_axes (len (values ),1 )
377
+ return _arrays_to_mgr ([ values ], columns , index , columns ,
378
+ dtype = dtype )
379
+
380
+ # by definition an array here
381
+ # the dtypes will be coerced to a single dtype
348
382
values = _prep_ndarray (values , copy = copy )
349
383
350
384
if dtype is not None :
385
+
351
386
if values .dtype != dtype :
352
387
try :
353
388
values = values .astype (dtype )
@@ -356,18 +391,7 @@ def _init_ndarray(self, values, index, columns, dtype=None,
356
391
% (dtype , orig ))
357
392
raise_with_traceback (e )
358
393
359
- N , K = values .shape
360
-
361
- if index is None :
362
- index = _default_index (N )
363
- else :
364
- index = _ensure_index (index )
365
-
366
- if columns is None :
367
- columns = _default_index (K )
368
- else :
369
- columns = _ensure_index (columns )
370
-
394
+ index , columns = _get_axes (* values .shape )
371
395
return create_block_manager_from_blocks ([values .T ], [columns , index ])
372
396
373
397
@property
@@ -877,7 +901,7 @@ def to_records(self, index=True, convert_datetime64=True):
877
901
else :
878
902
ix_vals = [self .index .values ]
879
903
880
- arrays = ix_vals + [self [c ].values for c in self .columns ]
904
+ arrays = ix_vals + [self [c ].get_values () for c in self .columns ]
881
905
882
906
count = 0
883
907
index_names = list (self .index .names )
@@ -890,7 +914,7 @@ def to_records(self, index=True, convert_datetime64=True):
890
914
index_names = ['index' ]
891
915
names = index_names + lmap (str , self .columns )
892
916
else :
893
- arrays = [self [c ].values for c in self .columns ]
917
+ arrays = [self [c ].get_values () for c in self .columns ]
894
918
names = lmap (str , self .columns )
895
919
896
920
dtype = np .dtype ([(x , v .dtype ) for x , v in zip (names , arrays )])
@@ -4729,6 +4753,7 @@ def convert(v):
4729
4753
values = convert (values )
4730
4754
4731
4755
else :
4756
+
4732
4757
# drop subclass info, do not copy data
4733
4758
values = np .asarray (values )
4734
4759
if copy :
0 commit comments