Skip to content

Initializing category with single value raises AttributeError #12336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
toobaz opened this issue Feb 15, 2016 · 2 comments
Closed

Initializing category with single value raises AttributeError #12336

toobaz opened this issue Feb 15, 2016 · 2 comments
Labels
Bug Categorical Categorical Data Type
Milestone

Comments

@toobaz
Copy link
Member

toobaz commented Feb 15, 2016

    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.)

@jreback
Copy link
Contributor

jreback commented Feb 16, 2016

yeah looks buggy.

@jreback jreback added this to the Next Major Release milestone Feb 16, 2016
@jreback
Copy link
Contributor

jreback commented Feb 16, 2016

obviously the expected result

In [2]: pd.Series(0, index=range(3)).astype("category")
Out[2]: 
0    0
1    0
2    0
dtype: category
Categories (1, int64): [0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type
Projects
None yet
2 participants