2

I'm using Python 3.6.2.

I've learnt from this question how to convert between the standard datetime type to np.datetime64 type, as follows.

dt = datetime.now()
print(dt)
print(np.datetime64(dt))

Output:

2017-12-19 17:20:12.743969
2017-12-19T17:20:12.743969

But I can't convert an iterable of standard datetime objects into a Numpy array. The following code ...

np.fromiter([dt], dtype=np.datetime64)

... gives the following error.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-46e4618bda89> in <module>()
----> 1 np.fromiter([dt], dtype=np.datetime64)

TypeError: Cannot cast datetime.datetime object from metadata [us] to  according to the rule 'same_kind'

However, using np.asarray() works.

np.asarray([dt])

Output:

array([datetime.datetime(2017, 12, 19, 17, 20, 12, 743969)], dtype=object)

Might this be a bug with either np.fromiter() or np.datetime64?

Ray
  • 7,833
  • 13
  • 57
  • 91

1 Answers1

2

It may just be a matter of setting the datetime units:

In [368]: dt = datetime.now()
In [369]: dt
Out[369]: datetime.datetime(2017, 12, 19, 12, 48, 45, 143287)

Default action for np.array (don't really need fromiter with a list) is to create an object dtype array:

In [370]: np.array([dt,dt])
Out[370]: 
array([datetime.datetime(2017, 12, 19, 12, 48, 45, 143287),
       datetime.datetime(2017, 12, 19, 12, 48, 45, 143287)], dtype=object)

Looks like plain 'datetime64' produces days:

In [371]: np.array([dt,dt], dtype='datetime64')
Out[371]: array(['2017-12-19', '2017-12-19'], dtype='datetime64[D]')

and specifying the units:

In [373]: np.array([dt,dt], dtype='datetime64[m]')
Out[373]: array(['2017-12-19T12:48', '2017-12-19T12:48'], dtype='datetime64[m]')

This also works with fromiter.

In [374]: np.fromiter([dt,dt], dtype='datetime64[m]')
Out[374]: array(['2017-12-19T12:48', '2017-12-19T12:48'], dtype='datetime64[m]')
In [384]: x= np.fromiter([dt,dt], dtype='M8[us]')
In [385]: x
Out[385]: array(['2017-12-19T12:48:45.143287', '2017-12-19T12:48:45.143287'], dtype='datetime64[us]')

I've learned to use the string name of the datetime64, which allows me to specify the units, rather than the most generic np.datetime64.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Thank you. I think the problem was that I didn't specify the resolution (unit). Using the string form works. – Ray Dec 20 '17 at 17:24