How to group categorical values in Pandas?
Question: I'm trying Converting to categorical value and grouping in pandas.

For example, I have tried the following:

    
    
    import pandas as pd
    
    df = pd.DataFrame()
    df['A'] = ['C1', 'C1', 'C2', 'C2', 'C3', 'C3']
    df['B'] = [1,2,3,4,5,6]
    
    df['A'] = df.loc[:,'A'].astype('category')
    
    df2 = df[0:3]
    
    result = df2.groupby(by='A')['B'].nunique()
    
    print(result)
    

Unfortunately, I get the exception

> File "C:\Python34\lib\site-packages\pandas\core\internals.py", line 86, in
> **init** len(self.values), len(self.mgr_locs)))
>
> ValueError: Wrong number of items passed 2, placement implies 3

**Edit** Unfortunately, the workaround suggested by @joris does not work for
my application. New counterexample:

    
    
    import pandas as pd
    
    df = pd.DataFrame()
    df['A'] = ['C1', 'C1', 'C2', pd.np.nan, 'C3', 'C3']
    df['B'] = [1,2,3,4,5,6]
    
    df['A'] = df.loc[:,'A'].astype('category')
    
    df2 = df[0:4]
    
    df2['A'] = df2['A'].cat.remove_unused_categories()
    
    result = df2.groupby(by='A')['B'].nunique()
    
    print(result)
    


Answer: As mentioned in the comments, this is a regression in pandas 0.17.0, and
reported here: <https://github.com/pydata/pandas/issues/11635>

As a workaround for now, you can easily use the `nunique` Series method
through `apply` instead of calling it directly on the groupby object:

    
    
    In [22]: df2.groupby(by='A')['B'].apply(lambda x: x.nunique())
    Out[22]:
    A
    C1    2
    C2    1
    C3    0
    Name: B, dtype: int64
    

The other problem you encountered with `remove_unused_categories()` is also a
bug, and this will be fixed in 0.17.1
(<https://github.com/pydata/pandas/pull/11639>)

