Skip to content

Cannot remove_unused_categories for CategoricalIndex in a MultiIndex #30846

Open
@fujiisoup

Description

@fujiisoup

I'm working to add a CategoricalIndex support in xarray (pydata/xarray#3647, pydata/xarray#3670), but have a trouble.

Code Sample, a copy-pastable example if possible

In [39]: cat = pd.CategoricalDtype(categories=['foo', 'bar', 'baz']) 
    ...: i1 = pd.Series(['foo', 'bar'], dtype=cat) 
    ...: i2 = pd.Series(['bar', 'bar'], dtype=cat) 
    ...: df = pd.DataFrame({'i1': i1, 'i2': i2, 'values': [1, 2]}) 

In [40]: df1 = df.set_index('i1')                                               
In [42]: df1.index                                                              
Out[42]: CategoricalIndex(['foo', 'bar'], 
        categories=['foo', 'bar', 'baz'], ordered=False, name='i1', dtype='category')

# remove_unused_categories() is working
In [43]: df1.index.remove_unused_categories()                                   
Out[43]: CategoricalIndex(['foo', 'bar'], 
        categories=['foo', 'bar'], ordered=False, name='i1', dtype='category')

In [44]: df2 = df.set_index(['i1', 'i2'])                                       
In [45]: df2.index                                                              
Out[45]: 
MultiIndex([('foo', 'bar'),
            ('bar', 'bar')],
           names=['i1', 'i2'])

# remove_unused_categories() is NOT working
In [47]: df2.index.levels[0].remove_unused_categories()                         
Out[47]: CategoricalIndex(['foo', 'bar', 'baz'], 
        categories=['foo', 'bar', 'baz'], ordered=False, name='i1', dtype='category')  # <-- `baz` is not removed.

Problem description

When CategoricalIndex is a level variable of MultiIndex, remove_unused_categories does not work.

Expected Output

Out[43]: CategoricalIndex(['foo', 'bar'], categories=['foo', 'bar'], ordered=False, name='i1', dtype='category')

Output of pd.show_versions()

In [48]: pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.5.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-1066-oem
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0.25.3
numpy : 1.17.4
pytz : 2019.3
dateutil : 2.8.1
pip : 19.3.1
setuptools : 42.0.2.post20191203
Cython : None
pytest : 5.3.2
hypothesis : 4.54.2
sphinx : 2.3.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.10.2
pandas_datareader: None
bs4 : None
bottleneck : 1.3.1
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.3.2
sqlalchemy : None
tables : None
xarray : 0.14.1
xlrd : 1.2.0
xlwt : None
xlsxwriter : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions