Skip to content

Construction of series with na and datetime dtype #46068

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

4 changes: 4 additions & 0 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
)
from pandas.core.dtypes.dtypes import (
DatetimeTZDtype,
IntervalDtype,
PandasDtype,
)
from pandas.core.dtypes.generic import (
Expand Down Expand Up @@ -774,6 +775,9 @@ def _try_cast(
return maybe_cast_to_datetime(arr, dtype)
# TODO: copy?

if isinstance(dtype, IntervalDtype) and isna(arr).all():
return np.asarray(arr).astype(IntervalDtype, copy=False)
Copy link
Member

Choose a reason for hiding this comment

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

this will always raise won't it?


array_type = dtype.construct_array_type()._from_sequence
subarr = array_type(arr, dtype=dtype, copy=copy)
return subarr
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1995,3 +1995,11 @@ def test_numpy_array(input_dict, expected):
def test_numpy_array_np_v1p19():
with pytest.raises(KeyError, match="0"):
np.array([Series({1: 1})])


def test_series_with_NAs_and_interval_of_datetime_dtype():
# GH#41805
result = Series(data=[None], dtype="interval[datetime64[ns]]")
expected = Series(np.nan, dtype="interval[datetime64[ns]]")

tm.assert_series_equal(result, expected)