Say, I have a datetime:
given_time = datetime(2013, 10, 8, 0, 0, 33, 945109,
tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=60,
name=None))
I would like to transform it into np.datetime64:
np.datetime64(given_time)
> numpy.datetime64('2013-10-08T00:00:33.945109+0100')
It works well. However, if I have an array of given_time:
given_times = np.array([given_time]*3) # dtype is object
Both given_times.astype('datetime64') and given_times = np.array([given_time] * 3, dtype=np.datetime64) would trigger TypeError: Cannot cast datetime.datetime object from metadata [us] to [D] according to the rule 'same_kind'
So, I have to specify the unit:
given_times.astype('datetime64[us]')
# or
given_times = np.array([given_time]*3, dtype='datetime64[us]')
My question is, why do I have to specify the unit here? It doesn't require unit in np.datatime64 constructor.
array.array.np.array(['2007-07-13', '2006-01-13', '2010-08-13'], dtype='datetime64')np.array(np.datetime64(d) for d in [given_time] * 3)?np.array(list(np.datetime64(d) for d in [given_time] * 3))works