Repository: pandas-dev/pandas
Pull request: BUG: conversion of Series to Categorical
Patch to bug fix #16524

Problem statement:
Converting Series to categorical
When converting `Series` to `categorical` dtype, the following method

```python

pd.Series(pd.Series(['a', 'b', 'c']), dtype='category')

```

fails with  traceback

```pytb
---------------------------------------------------------------------------
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.

<details>
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
</details>

Hints from issue discussion:
yeah this should work. the ``riase_on_error`` arg should not be passed thru here (we changing this arg in the future anyhow)

PR's are welcome.

Commit message: DOC: move release note

Patch to review:
diff --git a/doc/source/whatsnew/v0.20.3.txt b/doc/source/whatsnew/v0.20.3.txt
index 2032209c4aa23..64dfe02673e77 100644
--- a/doc/source/whatsnew/v0.20.3.txt
+++ b/doc/source/whatsnew/v0.20.3.txt
@@ -44,6 +44,7 @@ Conversion
 ^^^^^^^^^^
 
 - Bug in pickle compat prior to the v0.20.x series, when ``UTC`` is a timezone in a Series/DataFrame/Index (:issue:`16608`)
+- Bug in ``Series`` with ``dtype='category'`` (:issue:`16524`)
 
 Indexing
 ^^^^^^^^
diff --git a/pandas/core/internals.py b/pandas/core/internals.py
index 58690ad632152..f2a7ac76481d4 100644
--- a/pandas/core/internals.py
+++ b/pandas/core/internals.py
@@ -471,7 +471,7 @@ def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
                             **kwargs)
 
     def _astype(self, dtype, copy=False, errors='raise', values=None,
-                klass=None, mgr=None, **kwargs):
+                klass=None, mgr=None, raise_on_error=False, **kwargs):
         """
         Coerce to the new type (if copy=True, return a new copy)
         raise on an except if raise == True
diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py
index e084fa58d6c51..9ab02a8c2aad7 100644
--- a/pandas/tests/series/test_dtypes.py
+++ b/pandas/tests/series/test_dtypes.py
@@ -248,3 +248,12 @@ def test_intercept_astype_object(self):
 
         result = df.values.squeeze()
         assert (result[:, 0] == expected.values).all()
+
+    def test_series_to_categorical(self):
+        # see gh-16524: test conversion of Series to Categorical
+        series = Series(['a', 'b', 'c'])
+
+        result = Series(series, dtype='category')
+        expected = Series(['a', 'b', 'c'], dtype='category')
+
+        tm.assert_series_equal(result, expected)


Code review comments:
diff_hunk: @@ -44,6 +44,7 @@ Conversion
 ^^^^^^^^^^
 
 - Bug in pickle compat prior to the v0.20.x series, when ``UTC`` is a timezone in a Series/DataFrame/Index (:issue:`16608`)
+- Bug in ``Series`` with ``dtype='category'`` (:issue:`16524`)
path: doc/source/whatsnew/v0.20.3.txt
text: @user1:
be a bit more descriptive.

``Bug in Series construction when passing type=category``