Skip to content

BUG: Change of behavior in casting of datetime-like types in MultiIndex #44081

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 12 commits into from
9 changes: 8 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations

from datetime import datetime
from datetime import (
date,
datetime,
)
import functools
from itertools import zip_longest
import operator
Expand Down Expand Up @@ -764,7 +767,11 @@ def _shallow_copy(self: _IndexT, values, name: Hashable = no_default) -> _IndexT
name : Label, defaults to self.name
"""
name = self._name if name is no_default else name
u = self._simple_new(values, name=name)
if all(isinstance(x, date) for x in u):
Copy link
Member

Choose a reason for hiding this comment

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

this check absolutely doesn't belong here

from pandas import to_datetime

return to_datetime(u)
return self._simple_new(values, name=name)

def _view(self: _IndexT) -> _IndexT:
Expand Down
25 changes: 24 additions & 1 deletion pandas/tests/series/methods/test_reset_index.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from datetime import datetime
from datetime import (
date,
datetime,
)

import numpy as np
import pytest
Expand Down Expand Up @@ -179,3 +182,23 @@ def test_reset_index_dtypes_on_empty_series_with_multiindex(array, dtype):
{"level_0": np.int64, "level_1": np.float64, "level_2": dtype, 0: object}
)
tm.assert_series_equal(result, expected)


def test_set_index_MultiIndex():
df = DataFrame(
Copy link
Member

Choose a reason for hiding this comment

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

since you're testing the DataFrame behavior, this probably goes in the tests/frame/methods file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually in this file, there are a lot of tests regarding setting of indexes, hence I have kept this file.

{
"date": [date(2021, 8, 1), date(2021, 8, 2), date(2021, 8, 3)],
"ticker": ["aapl", "goog", "yhoo"],
"value": [5.63269, 4.45609, 2.74843],
}
)

df.set_index(["date", "ticker"], inplace=True)
res = df.index.get_level_values(0)
ex = pd.DatetimeIndex(
["2021-08-01", "2021-08-02", "2021-08-03"],
dtype="datetime64[ns]",
name="date",
freq=None,
)
tm.assert_index_equal(ex, res)