Skip to content

DOC: Inconsistencies in converting array with datetime values into object array #19782

@hec10r

Description

@hec10r

When converting a datetime64[ns] array into object type, it returns the date as an integer instead of datetime.datetime object

Reproducing code example:

>>> import numpy as np
>>> from datetime import datetime
>>> d = datetime.now()
>>> np.array(d)
array(datetime.datetime(2021, 8, 29, 20, 1, 32, 350877), dtype=object)
>>> np.array(d, dtype="datetime64[ns]")
array('2021-08-29T20:01:32.350877000', dtype='datetime64[ns]')
>>> np.array(d).astype("O") # Keeps the same, as expected
array(datetime.datetime(2021, 8, 29, 20, 1, 32, 350877), dtype=object)
>>> np.array(d, dtype="datetime64[ns]").astype("O") # Transforms to int. Unexpected
array(1630267292350877000, dtype=object)
>>> np.array(d, dtype="datetime64[ns]").view(np.int64) # Transforms to int. Expected
array(1630267292350877000)

IMO, the results from np.array(d).astype("O") and np.array(d, dtype="datetime64[ns]").astype("O") should be equal

NumPy/Python version information:

1.18.5 3.8.3 (default, Jul  2 2020, 11:26:31) 
[Clang 10.0.0 ]

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