Skip to content

BUG: Allow ensure_index to coerce nan to NaT with numpy object array and tz Timestamp #27556

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
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.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Timedelta
Timezones
^^^^^^^^^

-
- Bug in :class:`Index` where a numpy object array with a timezone aware :class:`Timestamp` and ``np.nan`` would not return a :class:`DatetimeIndex` (:issue:`27011`)
-
-

Expand Down
20 changes: 8 additions & 12 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,19 +469,15 @@ def __new__(
pass
elif inferred != "string":
if inferred.startswith("datetime"):
if (
lib.is_datetime_with_singletz_array(subarr)
or "tz" in kwargs
):
# only when subarr has the same tz
from pandas import DatetimeIndex
from pandas import DatetimeIndex

try:
return DatetimeIndex(
subarr, copy=copy, name=name, **kwargs
)
except OutOfBoundsDatetime:
pass
try:
return DatetimeIndex(subarr, copy=copy, name=name, **kwargs)
except (ValueError, OutOfBoundsDatetime):
# GH 27011
# If we have mixed timezones, just send it
# down the base constructor
pass

elif inferred.startswith("timedelta"):
from pandas import TimedeltaIndex
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/arrays/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ class TestAttributes:
(0, 1),
(Timedelta("0 days"), Timedelta("1 day")),
(Timestamp("2018-01-01"), Timestamp("2018-01-02")),
pytest.param(
(
Timestamp("2018-01-01", tz="US/Eastern"),
Timestamp("2018-01-02", tz="US/Eastern"),
marks=pytest.mark.xfail(strict=True, reason="GH 27011"),
),
],
)
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexes/datetimes/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,12 @@ def test_constructor_wrong_precision_raises(self):
with pytest.raises(ValueError):
pd.DatetimeIndex(["2000"], dtype="datetime64[us]")

def test_index_constructor_with_numpy_object_array_and_timestamp_tz_with_nan(self):
# GH 27011
result = Index(np.array([Timestamp("2019", tz="UTC"), np.nan], dtype=object))
expected = DatetimeIndex([Timestamp("2019", tz="UTC"), pd.NaT])
tm.assert_index_equal(result, expected)


class TestTimeSeries:
def test_dti_constructor_preserve_dti_freq(self):
Expand Down