Skip to content

Fix regression on datetime in MultiIndex #35140

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 4 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,9 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
if len(key) == labels.nlevels:
return {"key": key}
raise
except InvalidIndexError:
if not isinstance(labels, ABCMultiIndex):
raise
except TypeError:
pass
except ValueError:
Expand Down
24 changes: 23 additions & 1 deletion pandas/tests/indexing/multiindex/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@

import numpy as np

from pandas import Index, Period, Series, period_range
from pandas import (
DataFrame,
DatetimeIndex,
Index,
MultiIndex,
Period,
Series,
period_range,
to_datetime,
)


def test_multiindex_period_datetime():
Expand All @@ -20,3 +29,16 @@ def test_multiindex_period_datetime():
# try datetime as index
result = s.loc["a", datetime(2012, 1, 1)]
assert result == expected


def test_multiindex_datetime_columns():
# GH35015, using datetime as column indices raises exception

mi = MultiIndex.from_tuples(
[(to_datetime("02/29/2020"), to_datetime("03/01/2020"))], names=["a", "b"]
)

df = DataFrame([], columns=mi)
assert list(df.columns.names) == ["a", "b"]

assert all(isinstance(mi.get_level_values(i), DatetimeIndex) for i in range(2))