You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
NaN should become NaT when assigned to datetime64 #8780
When an array has dtype datetime64, setting to NaN should convert to NaT. Currently, it fails with ValueError:
In [49]: x = numpy.zeros(dtype="M8[s]", shape=5)
In [51]: x[2] = numpy.nan
…
ValueError: Could not convert object to NumPy datetime
The instruction = numpy.nan is not ambiguous in any way. When setting all the values of a structured dtype with floats and datetime types to nan it is much easier if we don't need to separately handle the case for nat.
So, I assume this is a request for enhanced NaN / NaT handling, not trying to sensibly handle arbitrary float input to np.datetime64. Integers are handled by adding to the unix epoch it seems.
Taking a step back here - is there any reason why we should support using np.nan here, given that in general we don't support floats? It seems like we should be encouraging np.nat instead
I don't have a strong view here -- just trying to chip away at open issues related to dtype things; do feel free to propose that we simply go with wontfix and close my PR or raise a different exception if you prefer.
There's a comment suggesting the correct behavior in the linked PR I've now closed, and the title of this issue is now apparently the behavior we don't want to allow.
If there's consensus on that, someone is free to make a new PR with the error handling / behavior enforcement when trying to do this kind of assignment.
Activity
tylerjereddy commentedon Aug 31, 2018
Is this something the core devs would like to see? The reported "issue" is still true as of the latest master (44d3b59) when I try the above example.
charris commentedon Aug 31, 2018
That sounds correct to me. How are floats converted in general?
tylerjereddy commentedon Aug 31, 2018
@charris They aren't:
The following attempts also fail with the same
ValueErrorreported above:tylerjereddy commentedon Aug 31, 2018
So, I assume this is a request for enhanced NaN / NaT handling, not trying to sensibly handle arbitrary float input to
np.datetime64. Integers are handled by adding to the unix epoch it seems.eric-wieser commentedon Sep 9, 2018
Taking a step back here - is there any reason why we should support using
np.nanhere, given that in general we don't support floats? It seems like we should be encouragingnp.natinsteadtylerjereddy commentedon Sep 10, 2018
I don't have a strong view here -- just trying to chip away at open issues related to dtype things; do feel free to propose that we simply go with wontfix and close my PR or raise a different exception if you prefer.
eric-wieser commentedon Mar 2, 2019
Worth noting the assignment works just fine if you use custom casting rules
It's a shame the error message isn't better.
tylerjereddy commentedon Mar 15, 2019
There's a comment suggesting the correct behavior in the linked PR I've now closed, and the title of this issue is now apparently the behavior we don't want to allow.
If there's consensus on that, someone is free to make a new PR with the error handling / behavior enforcement when trying to do this kind of assignment.
hameerabbasi commentedon Jan 29, 2020
I agree that we shouldn't have more cases of value-based casting.