M.E.
M.E.

Reputation: 5527

How do I generate a datetime64[ns, UTC] numpy series?

I am trying to generate a numpy series of type datetime64[ns, UTC] without success.

This is what I have tried:

test = np.array(np.datetime64('2005-01-03 14:30:00.000000000'))
test
>array('2005-01-03T14:30:00.000000000', dtype='datetime64[ns]')

And to convert, without success:

test = np.array(np.datetime64('2005-01-03 14:30:00.000000000').tz_localize('UTC'))
test
>>>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-23-0efefc078d50> in <module>
----> 1 test = np.array(np.datetime64('2005-01-03 14:30:00.000000000').tz_localize('UTC'))
      2 test

AttributeError: 'numpy.datetime64' object has no attribute 'tz_localize'

Upvotes: 2

Views: 4376

Answers (1)

Orkhan Sadigov
Orkhan Sadigov

Reputation: 66

I was able to achieve this result:

0   2005-01-03 14:30:00+00:00
dtype: datetime64[ns, UTC]

using the code below:

import numpy as np
import pandas as pd
s = pd.Series(np.datetime64('2005-01-03 14:30:00.000000000'))
s.dt.tz_localize('UTC')

Upvotes: 3

Related Questions