Search code examples
pythonpandasadditioncategorical-datain-place

ValueError: cannot use inplace with CategoricalIndex


I am using pandas 0.18.

This fails

 cat_fields[f[0]].add_categories(s,inplace=True)

However the docs say

inplace : boolean (default: False)

Whether or not to add the categories inplace or return a copy of this categorical with added categories.

Am I missing something ?

I am creating a superset of categories/columns across many dataframes to eventually be able to concatenate them.

My error:

ValueError: cannot use inplace with CategoricalIndex


Solution

  • I think you need assign to original column, because Series.add_categories has inplace parameter and it works nice.

    But in CategoricalIndex.add_categories has also inplace parameter, but it fails. I think it is bug.

    cat_fields[f[0]] = cat_fields[f[0]].add_categories(s)
    

    or:

    cat_fields[f[0]] = cat_fields[f[0]].cat.add_categories(s)
    

    Sample:

    cat_fields = pd.DataFrame({'A':[1,2,3]}, index=['a','d','f'])
    
    
    cat_fields.index = pd.CategoricalIndex(cat_fields.index)
    cat_fields.A = pd.Categorical(cat_fields.A)
    print (cat_fields)
       A
    a  1
    d  2
    f  3
    
    s = ['b','c']
    
    cat_fields.A.cat.add_categories(s,inplace=True)
    
    print (cat_fields.A)
    Name: A, dtype: category
    Categories (5, object): [1, 2, 3, b, c]
    
    
    cat_fields.index.add_categories(s,inplace=True)
    print (cat_fields)
    

    ValueError: cannot use inplace with CategoricalIndex