-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[ ] (optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
import pandas as pd
print(pd.Series(data=[None], dtype='interval[datetime64[ns]]'))
Problem description
Instantiating a Series
with NA values in data
fails if dtype
is specified as interval[datetime64[ns]]
, raising an error:
Expected Output
0 NaN
dtype: interval
Output of pd.show_versions()
Comment From: mzeitlin11
Thanks for reporting this @tlaytongoogle! Could you please add an informative title to the issue?
Comment From: tlaytongoogle
Title added.
Comment From: weikhor
Hi, I try to work on this problem. I am not sure whether this is considered bug. I notice that in this file pandas\tests\indexes\interval\test_astype.py, the function
index = IntervalIndex.from_arrays(
[-1.5, np.nan, 0.0, 0.0, 1.5], [-0.5, np.nan, 1.0, 1.0, 3.0], closed="both"
)
@pytest.mark.parametrize("subtype", ["datetime64[ns]", "timedelta64[ns]"])
def test_subtype_datetimelike(self, index, subtype):
dtype = IntervalDtype(subtype, "right")
msg = "Cannot convert .* to .*; subtypes are incompatible"
with pytest.raises(TypeError, match=msg):
index.astype(dtype)
The series [-1.5, np.nan, 0.0, 0.0, 1.5] in IntervalIndex index contains nan values. Based on test case, the expected result is TypeError: Cannot convert Float64Index to dtype datetime64[ns]; integer values are required for conversion which is same result from issue reported here.
Thank
Comment From: tlaytongoogle
@weikhor Consider that, in contrast to that test, it is entirely possible to convert the values from a Float64Index
(including NaN values) into datetime64[ns]
values via something like:
DatetimeIndex(index.left, dtype="datetime64[ns]") # index.left is a Float64Index
The key point here is that passing values into a constructor is not quite equivalent to casting with astype
. Thus, I don't think that this test is in-and-of-itself evidence that the reported issue constitutes intended behavior.