1919 is_list_like , _values_from_object ,
2020 _possibly_cast_to_datetime , _possibly_castable ,
2121 _possibly_convert_platform , _try_sort ,
22- ABCSparseArray , _maybe_match_name ,
22+ ABCSparseArray , _maybe_match_name , _coerce_to_dtype ,
2323 _ensure_object , SettingWithCopyError )
2424from pandas .core .index import (Index , MultiIndex , InvalidIndexError ,
2525 _ensure_index )
@@ -2434,7 +2434,7 @@ def _sanitize_array(data, index, dtype=None, copy=False,
24342434 """ sanitize input data to an ndarray, copy if specified, coerce to the dtype if specified """
24352435
24362436 if dtype is not None :
2437- dtype = np . dtype (dtype )
2437+ dtype = _coerce_to_dtype (dtype )
24382438
24392439 if isinstance (data , ma .MaskedArray ):
24402440 mask = ma .getmaskarray (data )
@@ -2455,9 +2455,11 @@ def _try_cast(arr, take_fast_path):
24552455 arr = _possibly_cast_to_datetime (arr , dtype )
24562456 subarr = pa .array (arr , dtype = dtype , copy = copy )
24572457 except (ValueError , TypeError ):
2458- if dtype is not None and raise_cast_failure :
2458+ if com .is_categorical_dtype (dtype ):
2459+ subarr = Categorical (arr )
2460+ elif dtype is not None and raise_cast_failure :
24592461 raise
2460- else : # pragma: no cover
2462+ else :
24612463 subarr = pa .array (arr , dtype = object , copy = copy )
24622464 return subarr
24632465
0 commit comments