def __init__(self, values, categories=None, ordered=None, dtype=None, fastpath=False):
    if (dtype is not None):
        if isinstance(dtype, compat.string_types):
            if (dtype == 'category'):
                dtype = CategoricalDtype(categories, ordered)
            else:
                msg = 'Unknown `dtype` {dtype}'
                raise ValueError(msg.format(dtype=dtype))
        elif ((categories is not None) or (ordered is not None)):
            raise ValueError('Cannot specify both `dtype` and `categories` or `ordered`.')
        categories = dtype.categories
    elif is_categorical(values):
        dtype = values.dtype._from_categorical_dtype(values.dtype, categories, ordered)
    else:
        dtype = CategoricalDtype(categories, ordered)
    if fastpath:
        self._codes = coerce_indexer_dtype(values, categories)
        self._dtype = self._dtype.update_dtype(dtype)
        return
    null_mask = np.array(False)
    if is_categorical_dtype(values):
        if (dtype.categories is None):
            dtype = CategoricalDtype(values.categories, dtype.ordered)
    elif (not isinstance(values, (ABCIndexClass, ABCSeries))):
        values = maybe_infer_to_datetimelike(values, convert_dates=True)
        if (not isinstance(values, np.ndarray)):
            values = _convert_to_list_like(values)
            from pandas.core.series import _sanitize_array
            if (len(values) == 0):
                sanitize_dtype = 'object'
            else:
                sanitize_dtype = None
            null_mask = isna(values)
            if null_mask.any():
                values = [values[idx] for idx in np.where((~ null_mask))[0]]
            values = _sanitize_array(values, None, dtype=sanitize_dtype)
    if (dtype.categories is None):
        try:
            (codes, categories) = factorize(values, sort=True)
        except TypeError:
            (codes, categories) = factorize(values, sort=False)
            if dtype.ordered:
                raise TypeError("'values' is not ordered, please explicitly specify the categories order by passing in a categories argument.")
        except ValueError:
            raise NotImplementedError('> 1 ndim Categorical are not supported at this time')
        dtype = CategoricalDtype(categories, dtype.ordered)
    elif is_categorical_dtype(values):
        old_codes = (values.cat.codes if isinstance(values, ABCSeries) else values.codes)
        codes = _recode_for_categories(old_codes, values.dtype.categories, dtype.categories)
    else:
        codes = _get_codes_for_values(values, dtype.categories)
    if null_mask.any():
        full_codes = (- np.ones(null_mask.shape, dtype=codes.dtype))
        full_codes[(~ null_mask)] = codes
        codes = full_codes
    self._dtype = self._dtype.update_dtype(dtype)
    self._codes = coerce_indexer_dtype(codes, dtype.categories)
def __init__(self, values, categories=None, ordered=None, dtype=None, fastpath=False):
    if (dtype is not None):
        if isinstance(dtype, compat.string_types):
            if (dtype == 'category'):
                dtype = CategoricalDtype(categories, ordered)
            else:
                msg = 'Unknown `dtype` {dtype}'
                raise ValueError(msg.format(dtype=dtype))
        elif ((categories is not None) or (ordered is not None)):
            raise ValueError('Cannot specify both `dtype` and `categories` or `ordered`.')
        categories = dtype.categories
    elif is_categorical(values):
        dtype = values.dtype._from_categorical_dtype(values.dtype, categories, ordered)
    else:
        dtype = CategoricalDtype(categories, ordered)
    if fastpath:
        self._codes = coerce_indexer_dtype(values, categories)
        self._dtype = self._dtype.update_dtype(dtype)
        return
    null_mask = np.array(False)
    if is_categorical_dtype(values):
        if (dtype.categories is None):
            dtype = CategoricalDtype(values.categories, dtype.ordered)
    elif (not isinstance(values, (ABCIndexClass, ABCSeries))):
        values = maybe_infer_to_datetimelike(values, convert_dates=True)
        if (not isinstance(values, np.ndarray)):
            values = _convert_to_list_like(values)
            from pandas.core.series import _sanitize_array
            if (len(values) == 0):
                sanitize_dtype = 'object'
            else:
                sanitize_dtype = None
            null_mask = isna(values)
            if null_mask.any():
                values = [values[idx] for idx in np.where((~ null_mask))[0]]
            values = _sanitize_array(values, None, dtype=sanitize_dtype)
    if (dtype.categories is None):
        try:
            (codes, categories) = factorize(values, sort=True)
        except TypeError:
            (codes, categories) = factorize(values, sort=False)
            if dtype.ordered:
                raise TypeError("'values' is not ordered, please explicitly specify the categories order by passing in a categories argument.")
        except ValueError:
            raise NotImplementedError('> 1 ndim Categorical are not supported at this time')
        dtype = CategoricalDtype(categories, dtype.ordered)
    elif is_categorical_dtype(values):
        old_codes = (values.cat.codes if isinstance(values, ABCSeries) else values.codes)
        codes = _recode_for_categories(old_codes, values.dtype.categories, dtype.categories)
    else:
        codes = _get_codes_for_values(values, dtype.categories)
    if null_mask.any():
        full_codes = (- np.ones(null_mask.shape, dtype=codes.dtype))
        full_codes[(~ null_mask)] = codes
        codes = full_codes
    self._dtype = self._dtype.update_dtype(dtype)
    self._codes = coerce_indexer_dtype(codes, dtype.categories)