Skip to content

BUG: MultiIndex.get_level_values created from Categorical raises AttributeError #10465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Bug Fixes

- Bug in ``DataFrame.interpolate`` with ``axis=1`` and ``inplace=True`` (:issue:`10395`)


- Bug in ``MultiIndex.get_level_values`` including ``Categorical`` raises ``AttributeError`` (:issue:`10460`)

- Bug in ``test_categorical`` on big-endian builds (:issue:`10425`)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4238,7 +4238,7 @@ def get_level_values(self, level):
num = self._get_level_number(level)
unique = self.levels[num] # .values
labels = self.labels[num]
filled = com.take_1d(unique.values, labels, fill_value=unique._na_value)
filled = com.take_1d(unique.get_values(), labels, fill_value=unique._na_value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you fix like i suggest in #10464 I think this will be fixed for free

values = unique._simple_new(filled, self.names[num],
freq=getattr(unique, 'freq', None),
tz=getattr(unique, 'tz', None))
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3476,6 +3476,13 @@ def test_groupby_categorical(self):
expected.index.names = ['myfactor', None]
assert_frame_equal(desc_result, expected)

# GH 10460
exp = CategoricalIndex(['foo'] * 8 + ['bar'] * 8 + ['baz'] * 8 + ['qux'] * 8,
name='myfactor')
self.assert_index_equal(desc_result.index.get_level_values(0), exp)
exp = Index(['count', 'mean', 'std', 'min', '25%', '50%', '75%', 'max'] * 4)
self.assert_index_equal(desc_result.index.get_level_values(1), exp)

def test_groupby_datetime_categorical(self):
# GH9049: ensure backward compatibility
levels = pd.date_range('2014-01-01', periods=4)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3567,6 +3567,16 @@ def test_get_level_values_na(self):
values = index.get_level_values(0)
self.assertEqual(values.shape, (0,))

# GH 10460
index = MultiIndex(levels=[CategoricalIndex(['A', 'B']),
CategoricalIndex([1, 2, 3])],
labels=[np.array([0, 0, 0, 1, 1, 1]),
np.array([0, 1, 2, 0, 1, 2])])
exp = CategoricalIndex(['A', 'A', 'A', 'B', 'B', 'B'])
self.assert_index_equal(index.get_level_values(0), exp)
exp = CategoricalIndex([1, 2 ,3, 1, 2, 3])
self.assert_index_equal(index.get_level_values(1), exp)

def test_reorder_levels(self):
# this blows up
assertRaisesRegexp(IndexError, '^Too many levels',
Expand Down