user5779223
user5779223

Reputation: 1490

How to construct a numpy array with multilple same timestamp?

I have tried:

import numpy as np
t = np.full((10),'2012-12-10', dtype=np.datetime64)

But got such an error:

ValueError: Cannot create a NumPy datetime other than NaT with generic units

Do you have any idea? Thanks!

Upvotes: 0

Views: 133

Answers (1)

abnowack
abnowack

Reputation: 380

EDIT: I figured it out, you must fully specify the datatype for datetime objects

Do this

np.full((10), '2012-12-10', dtype='datetime64[D]')

array(['2012-12-10', '2012-12-10', '2012-12-10', '2012-12-10',
       '2012-12-10', '2012-12-10', '2012-12-10', '2012-12-10',
       '2012-12-10', '2012-12-10'], dtype='datetime64[D]')

My previous answer is below


Hm, not sure why np.full does not work in that case specifically. However one way to achieve this is to instead use np.tile

np.tile(np.array(['2012-12-10'], dtype=np.datetime64), 10)

array(['2012-12-10', '2012-12-10', '2012-12-10', '2012-12-10',
       '2012-12-10', '2012-12-10', '2012-12-10', '2012-12-10',
       '2012-12-10', '2012-12-10'], dtype='datetime64[D]')

Upvotes: 1

Related Questions