BUG/API: MultiIndex.get_level_values created from Categorical raises AttributeError · Issue #10460 · pandas-dev/pandas (original) (raw)

import pandas as pd
import numpy as np
levels = ['foo', 'bar', 'baz', 'qux']
codes = np.random.randint(0, 4, size=100)

cats = pd.Categorical.from_codes(codes, levels, name='myfactor', ordered=True)

data = pd.DataFrame(np.random.randn(100, 4))
grouped = data.groupby(cats)
desc_result = grouped.describe()
desc_result.index.get_level_values(0)
# AttributeError: 'Categorical' object has no attribute 'flags'

Changing below line to use .get_values() looks fix the issue.

desc_result.index.get_level_values(0)
CategoricalIndex([u'foo', u'foo', u'foo', u'foo', u'foo', u'foo', u'foo',
                  u'foo', u'bar', u'bar', u'bar', u'bar', u'bar', u'bar',
                  u'bar', u'bar', u'baz', u'baz', u'baz', u'baz', u'baz',
                  u'baz', u'baz', u'baz', u'qux', u'qux', u'qux', u'qux',
                  u'qux', u'qux', u'qux', u'qux'],
                 categories=[u'bar', u'baz', u'foo', u'qux'], ordered=False, name=u'myfactor', dtype='category')