Passing Series with dtype= 'category' as categories for Pandas Categorical function
<p>when I run this code I get the following error :</p>

<pre><code>import pandas as pd

car_colors = pd.Series(['Blue', 'Red', 'Green'],
           dtype='category')

car_data = pd.Categorical(['Yellow', 'Green', 'Red', 'Blue','Purple'],
                              categories= car_colors, ordered=False)
print car_colors
s = pd.Series(car_data)
s
</code></pre>

<blockquote>
  <p>ValueError: object <strong>array</strong> method not producing an array</p>
</blockquote>

<p>But the funny thing is, when I remove the <code>dtype = 'category'</code>, the code works fine.</p>

<p>So in short, the categorical function is accepting series but not with <code>dtype = 'category'</code></p>

<p>Is it a bug or am I doing something wrong ?</p>

<p>It looks like need add <code>tolist</code> to <code>categories</code> in <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Categorical.html" rel="nofollow"><code>Categorical</code></a>:</p>

<pre><code>car_colors = pd.Series(['Blue', 'Red', 'Green'],
           dtype='category')

car_data = pd.Categorical(['Yellow', 'Green', 'Red', 'Blue','Purple'],
                              categories = car_colors.tolist(), ordered=False)

s = pd.Series(car_data)
print (s)

0      NaN
1    Green
2      Red
3     Blue
4      NaN
dtype: category
Categories (3, object): [Blue, Red, Green]
</code></pre>

<p>Another solution from <a href="http://stackoverflow.com/questions/39467885/passing-series-with-dtype-category-as-categories-for-pandas-categorical-funct/39467943#comment66255855_39467885">EdChum's comment</a> is use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.cat.categories.html" rel="nofollow"><code>cat.categories</code></a>:</p>

<pre><code>car_data = pd.Categorical(['Yellow', 'Green', 'Red', 'Blue','Purple'],
                              categories = car_colors.cat.categories, ordered=False)
s = pd.Series(car_data)
print (s)
0      NaN
1    Green
2      Red
3     Blue
4      NaN
dtype: category
Categories (3, object): [Blue, Green, Red]
</code></pre>

python pandas categorical-data