Initializing category with single value raises AttributeError #12336

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

Comments

Projects
None yet
2 participants
Contributor

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

Contributor

jreback commented Feb 16, 2016

yeah looks buggy.

jreback added this to the Next Major Release milestone Feb 16, 2016

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]

@rinoc rinoc added a commit to rinoc/pandas that referenced this issue Feb 17, 2016

@rinoc rinoc BUG: Init categorical series with scalar value (#12336) acc7b01

@jreback jreback modified the milestone: 0.18.0, Next Major Release Feb 17, 2016

@rinoc rinoc added a commit to rinoc/pandas that referenced this issue Feb 17, 2016

@rinoc rinoc BUG: Init categorical series with scalar value (#12336)
Move and change tests.

BUG: Bug in DataFrame.set_index() with tz-aware Series

closes #12358

Author: Jeff Reback <jeff@reback.net>

Closes #12365 from jreback/set_index_tz and squashes the following commits:

46d5c9d [Jeff Reback] BUG: Bug in DataFrame.set_index() with tz-aware Series
555a76f

@rinoc rinoc added a commit to rinoc/pandas that referenced this issue Feb 17, 2016

@rinoc rinoc BUG #12336: Init categorical series from value
Improve test, add comment
2b643a9

jreback closed this in 672fb14 Feb 18, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment