Пример #1
0
    def __new__(
        cls,
        data=None,
        categories=None,
        ordered=None,
        dtype=None,
        copy=False,
        name=None,
    ):

        dtype = CategoricalDtype._from_values_or_dtype(data, categories, ordered, dtype)

        if name is None and hasattr(data, "name"):
            name = data.name

        if not is_categorical_dtype(data):
            # don't allow scalars
            # if data is None, then categories must be provided
            if is_scalar(data):
                if data is not None or categories is None:
                    raise cls._scalar_data_error(data)
                data = []

        data = cls._create_categorical(data, dtype=dtype)

        data = data.copy() if copy else data

        return cls._simple_new(data, name=name)
Пример #2
0
    def __new__(
        cls, data=None, categories=None, ordered=None, dtype=None, copy=False, name=None
    ):

        dtype = CategoricalDtype._from_values_or_dtype(data, categories, ordered, dtype)

        name = maybe_extract_name(name, data, cls)

        if not is_categorical_dtype(data):
            # don't allow scalars
            # if data is None, then categories must be provided
            if is_scalar(data):
                if data is not None or categories is None:
                    raise cls._scalar_data_error(data)
                data = []

        assert isinstance(dtype, CategoricalDtype), dtype
        data = extract_array(data, extract_numpy=True)

        if not isinstance(data, Categorical):
            data = Categorical(data, dtype=dtype)
        elif isinstance(dtype, CategoricalDtype) and dtype != data.dtype:
            # we want to silently ignore dtype='category'
            data = data._set_dtype(dtype)

        data = data.copy() if copy else data

        return cls._simple_new(data, name=name)
Пример #3
0
    def __new__(cls, data=None, categories=None, ordered=None, dtype=None,
                copy=False, name=None, fastpath=None):

        if fastpath is not None:
            warnings.warn("The 'fastpath' keyword is deprecated, and will be "
                          "removed in a future version.",
                          FutureWarning, stacklevel=2)
            if fastpath:
                return cls._simple_new(data, name=name, dtype=dtype)

        dtype = CategoricalDtype._from_values_or_dtype(data, categories,
                                                       ordered, dtype)

        if name is None and hasattr(data, 'name'):
            name = data.name

        if not is_categorical_dtype(data):
            # don't allow scalars
            # if data is None, then categories must be provided
            if is_scalar(data):
                if data is not None or categories is None:
                    cls._scalar_data_error(data)
                data = []

        data = cls._create_categorical(data, dtype=dtype)

        data = data.copy() if copy else data

        return cls._simple_new(data, name=name)
Пример #4
0
    def __new__(cls, data=None, categories=None, ordered=None, dtype=None,
                copy=False, name=None, fastpath=None):

        if fastpath is not None:
            warnings.warn("The 'fastpath' keyword is deprecated, and will be "
                          "removed in a future version.",
                          FutureWarning, stacklevel=2)
            if fastpath:
                return cls._simple_new(data, name=name, dtype=dtype)

        dtype = CategoricalDtype._from_values_or_dtype(data, categories,
                                                       ordered, dtype)

        if name is None and hasattr(data, 'name'):
            name = data.name

        if not is_categorical_dtype(data):
            # don't allow scalars
            # if data is None, then categories must be provided
            if is_scalar(data):
                if data is not None or categories is None:
                    cls._scalar_data_error(data)
                data = []

        data = cls._create_categorical(data, dtype=dtype)

        data = data.copy() if copy else data

        return cls._simple_new(data, name=name)
Пример #5
0
 def test_from_values_or_dtype_raises(self, values, categories, ordered,
                                      dtype):
     msg = "Cannot specify `categories` or `ordered` together with `dtype`."
     with pytest.raises(ValueError, match=msg):
         CategoricalDtype._from_values_or_dtype(values, categories, ordered,
                                                dtype)
Пример #6
0
 def test_from_values_or_dtype(self, values, categories, ordered, dtype,
                               expected):
     result = CategoricalDtype._from_values_or_dtype(
         values, categories, ordered, dtype)
     assert result == expected
Пример #7
0
 def test_from_values_or_dtype_invalid_dtype(self):
     msg = "Cannot not construct CategoricalDtype from <class 'object'>"
     with pytest.raises(ValueError, match=msg):
         CategoricalDtype._from_values_or_dtype(None, None, None, object)
Пример #8
0
 def test_from_values_or_dtype_raises(self, values, categories,
                                      ordered, dtype):
     msg = "Cannot specify `categories` or `ordered` together with `dtype`."
     with pytest.raises(ValueError, match=msg):
         CategoricalDtype._from_values_or_dtype(values, categories,
                                                ordered, dtype)
Пример #9
0
 def test_from_values_or_dtype(
         self, values, categories, ordered, dtype, expected):
     result = CategoricalDtype._from_values_or_dtype(values, categories,
                                                     ordered, dtype)
     assert result == expected