Skip to content

BUG: create new MI from MultiIndex._get_level_values #33134

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

Merged
merged 2 commits into from
Mar 31, 2020
Merged
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
4 changes: 2 additions & 2 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ def _formatter_func(self):

@cache_readonly
def _engine(self):
# To avoid a reference cycle, pass a weakref of self to _engine_type.
period = weakref.ref(self)
# To avoid a reference cycle, pass a weakref of self._values to _engine_type.
period = weakref.ref(self._values)
return self._engine_type(period, len(self))

@doc(Index.__contains__)
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/multi/test_get_level_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,17 @@ def test_get_level_values_na():
result = index.get_level_values(0)
expected = pd.Index([], dtype=object)
tm.assert_index_equal(result, expected)


def test_get_level_values_when_periods():
# GH33131. See also discussion in GH32669.
# This test can probably be removed when PeriodIndex._engine is removed.
from pandas import Period, PeriodIndex

idx = MultiIndex.from_arrays(
[PeriodIndex([Period("2019Q1"), Period("2019Q2")], name="b")]
)
idx2 = MultiIndex.from_arrays(
[idx._get_level_values(level) for level in range(idx.nlevels)]
)
assert all(x.is_monotonic for x in idx2.levels)