Skip to content

NaN should become NaT when assigned to datetime64 #8780

Open

Description

@gerritholl

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.

Activity

  1. tylerjereddy commented on Aug 31, 2018

    @tylerjereddy
    Contributor

    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.

  2. charris commented on Aug 31, 2018

    @charris
    Member

    That sounds correct to me. How are floats converted in general?

  3. tylerjereddy commented on Aug 31, 2018

    @tylerjereddy
    Contributor

    @charris They aren't:

    The following attempts also fail with the same ValueError reported above:

    x[2] = 2.5
    x = np.array([1.2, 3.5], dtype='M8[s]')
    # also for generic case:
    x = np.array([1.2, 3.5], dtype=np.datetime64)
  4. tylerjereddy commented on Aug 31, 2018

    @tylerjereddy
    Contributor

    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.

  5. eric-wieser commented on Sep 9, 2018

    @eric-wieser
    Member

    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

  6. tylerjereddy commented on Sep 10, 2018

    @tylerjereddy
    Contributor

    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.

  7. eric-wieser commented on Mar 2, 2019

    @eric-wieser
    Member

    Worth noting the assignment works just fine if you use custom casting rules

    In [4]: x = np.zeros(dtype="M8[s]", shape=5)
    
    In [5]: x[2] = np.nan
    Traceback (most recent call last):
      File "<ipython-input-5-6c5795fab603>", line 1, in <module>
        x[2] = np.nan
    ValueError: Could not convert object to NumPy datetime
    
    
    In [6]: np.copyto(x[2,...], np.nan)
    Traceback (most recent call last):
      File "<ipython-input-6-317f333b4fc9>", line 1, in <module>
        np.copyto(x[2,...], np.nan)
    TypeError: Cannot cast scalar from dtype('float64') to dtype('<M8[s]') according to the rule 'same_kind'
    
    
    In [7]: np.copyto(x[2,...], np.nan, casting='unsafe')
    
    In [8]: x
    Out[8]:
    array(['1970-01-01T00:00:00', '1970-01-01T00:00:00',
                           'NaT', '1970-01-01T00:00:00',
           '1970-01-01T00:00:00'], dtype='datetime64[s]')

    It's a shame the error message isn't better.

  8. tylerjereddy commented on Mar 15, 2019

    @tylerjereddy
    Contributor

    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.

  9. hameerabbasi commented on Jan 29, 2020

    @hameerabbasi
    Contributor

    I agree that we shouldn't have more cases of value-based casting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions