2626from pandas .core .common import (isnull , notnull , PandasError , _try_sort ,
2727 _default_index , _maybe_upcast , _is_sequence ,
2828 _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 )
3031from pandas .core .generic import NDFrame , _shared_docs
3132from pandas .core .index import Index , MultiIndex , _ensure_index
3233from pandas .core .indexing import (_maybe_droplevels ,
@@ -332,6 +333,8 @@ def _init_dict(self, data, index, columns, dtype=None):
332333
333334 def _init_ndarray (self , values , index , columns , dtype = None ,
334335 copy = False ):
336+ # input must be a ndarray, list, Series, index
337+
335338 if isinstance (values , Series ):
336339 if columns is None :
337340 if values .name is not None :
@@ -345,9 +348,41 @@ def _init_ndarray(self, values, index, columns, dtype=None,
345348 if not len (values ) and columns is not None and len (columns ):
346349 values = np .empty ((0 , 1 ), dtype = object )
347350
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
348382 values = _prep_ndarray (values , copy = copy )
349383
350384 if dtype is not None :
385+
351386 if values .dtype != dtype :
352387 try :
353388 values = values .astype (dtype )
@@ -356,18 +391,7 @@ def _init_ndarray(self, values, index, columns, dtype=None,
356391 % (dtype , orig ))
357392 raise_with_traceback (e )
358393
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 )
371395 return create_block_manager_from_blocks ([values .T ], [columns , index ])
372396
373397 @property
@@ -877,7 +901,7 @@ def to_records(self, index=True, convert_datetime64=True):
877901 else :
878902 ix_vals = [self .index .values ]
879903
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 ]
881905
882906 count = 0
883907 index_names = list (self .index .names )
@@ -890,7 +914,7 @@ def to_records(self, index=True, convert_datetime64=True):
890914 index_names = ['index' ]
891915 names = index_names + lmap (str , self .columns )
892916 else :
893- arrays = [self [c ].values for c in self .columns ]
917+ arrays = [self [c ].get_values () for c in self .columns ]
894918 names = lmap (str , self .columns )
895919
896920 dtype = np .dtype ([(x , v .dtype ) for x , v in zip (names , arrays )])
@@ -4729,6 +4753,7 @@ def convert(v):
47294753 values = convert (values )
47304754
47314755 else :
4756+
47324757 # drop subclass info, do not copy data
47334758 values = np .asarray (values )
47344759 if copy :
0 commit comments