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 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: 4 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,10 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
if len(key) == labels.nlevels:
return {"key": key}
raise
except InvalidIndexError:
# GH35015, using datetime as column indices raises exception
if not isinstance(labels, ABCMultiIndex):
raise
except TypeError:
pass
except ValueError:
Expand Down
30 changes: 29 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,
Index,
MultiIndex,
Period,
Series,
period_range,
to_datetime,
)
import pandas._testing as tm


def test_multiindex_period_datetime():
Expand All @@ -20,3 +29,22 @@ 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)

expected_df = DataFrame(
[],
columns=MultiIndex.from_arrays(
[[to_datetime("02/29/2020")], [to_datetime("03/01/2020")]], names=["a", "b"]
),
)

tm.assert_frame_equal(df, expected_df)