Skip to content

Initializing category with single value raises AttributeError #12336

Closed
@toobaz

Description

@toobaz
    In [2]: pd.Series([0,0,0], dtype="category")
    Out[2]: 
    0    0
    1    0
    2    0
    dtype: category
    Categories (1, int64): [0]

    In [3]: pd.Series(0, index=range(3), dtype="category")
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-3-2c1d7a241071> in <module>()
    ----> 1 pd.Series(0, index=range(3), dtype="category")

    /home/nobackup/repo/pandas/pandas/core/series.pyc in __init__(self, data, index, dtype, name, copy, fastpath)
        224             else:
        225                 data = _sanitize_array(data, index, dtype, copy,
    --> 226                                        raise_cast_failure=True)
        227 
        228                 data = SingleBlockManager(data, index, fastpath=True)

    /home/nobackup/repo/pandas/pandas/core/series.pyc in _sanitize_array(data, index, dtype, copy, raise_cast_failure)
       2961             if len(subarr) != len(index) and len(subarr) == 1:
       2962                 subarr = create_from_value(subarr[0], index,
    -> 2963                                            subarr.dtype)
       2964 
       2965     elif subarr.ndim > 1:

    /home/nobackup/repo/pandas/pandas/core/series.pyc in create_from_value(value, index, dtype)
       2929         else:
       2930             if not isinstance(dtype, (np.dtype, type(np.dtype))):
    -> 2931                 dtype = dtype.dtype
       2932             subarr = np.empty(len(index), dtype=dtype)
       2933             subarr.fill(value)

    AttributeError: 'CategoricalDtype' object has no attribute 'dtype'

(Not a particularly brilliant use of categories, but they can then be concatenated with others.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions