Skip to content

Converting Series to categorical #16524

Closed
@ttylec

Description

@ttylec

When converting Series to categorical dtype, the following method

pd.Series(pd.Series(['a', 'b', 'c']), dtype='category')

fails with traceback

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-6c903a3f0db3> in <module>()
----> 1 pd.Series(pd.Series(['a', 'b', 'c']), dtype='category')

/Users/ttylec/.virtualenvs/zettafox/lib/python3.6/site-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    241                 if dtype is not None:
    242                     data = data.astype(dtype=dtype, raise_on_error=False,
--> 243                                        copy=copy)
    244                 elif copy:
    245                     data = data.copy()

/Users/ttylec/.virtualenvs/zettafox/lib/python3.6/site-packages/pandas/core/internals.py in astype(self, dtype, **kwargs)
   3222 
   3223     def astype(self, dtype, **kwargs):
-> 3224         return self.apply('astype', dtype=dtype, **kwargs)
   3225 
   3226     def convert(self, **kwargs):

/Users/ttylec/.virtualenvs/zettafox/lib/python3.6/site-packages/pandas/core/internals.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
   3089 
   3090             kwargs['mgr'] = self
-> 3091             applied = getattr(b, f)(**kwargs)
   3092             result_blocks = _extend_blocks(applied, result_blocks)
   3093 

/Users/ttylec/.virtualenvs/zettafox/lib/python3.6/site-packages/pandas/core/internals.py in astype(self, dtype, copy, errors, values, **kwargs)
    469     def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
    470         return self._astype(dtype, copy=copy, errors=errors, values=values,
--> 471                             **kwargs)
    472 
    473     def _astype(self, dtype, copy=False, errors='raise', values=None,

/Users/ttylec/.virtualenvs/zettafox/lib/python3.6/site-packages/pandas/core/internals.py in _astype(self, dtype, copy, errors, values, klass, mgr, **kwargs)
    488         # this is only called for non-categoricals
    489         if self.is_categorical_astype(dtype):
--> 490             return self.make_block(Categorical(self.values, **kwargs))
    491 
    492         # astype processing

TypeError: __init__() got an unexpected keyword argument 'raise_on_error'

When using simple list or np.array instead of pd.Series, it work. Doing .astype('category') also works.

INSTALLED VERSIONS ------------------ commit: None python: 3.6.1.final.0 python-bits: 64 OS: Darwin OS-release: 15.6.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: en_US.UTF-8 LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.20.1
pytest: None
pip: 9.0.1
setuptools: 35.0.1
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
xarray: None
IPython: 5.3.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: 3.3.0
numexpr: 2.6.2
feather: None
matplotlib: 2.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugCategoricalCategorical Data TypeReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions