25

I basically face the same problem posted here:Converting between datetime, Timestamp and datetime64

but I couldn't find satisfying answer from it, my question how to extract datetime from numpy.datetime64 type:

if I try:

np.datetime64('2012-06-18T02:00:05.453000000-0400').astype(datetime.datetime)

it gave me: 1339999205453000000L

my current solution is convert datetime64 into a string and then turn to datetime again. but it seems quite a silly method.

4
  • 1
    That L value is the same you get from .tolist() Commented Apr 20, 2015 at 16:42
  • I found from the test file, that dtype 'M8[ms]' readily converts to datetime. Commented Apr 20, 2015 at 21:19
  • have your read my answer to the end? Have you noticed that different numpy versions behave differently here? The answer contains code for different numpy versions (the versions are explicitly specified). There is even code example that produces long number and then later is shown how to get datetime in this case. Commented Apr 20, 2015 at 23:08
  • I'm sorry but both the original answer and this one are confusing. There seem to be a lot of different ways of doing it and I'm still not any clearer on the question of how to convert a numpy datetime to a pandas date time. For example, I am not clear if the solution below is saying that: pdDateTime = datetime.datetime.utcfromtimestamp(x.tolist()/1e9) ?? Commented Jun 27, 2017 at 14:24

2 Answers 2

47

Borrowing from Converting between datetime, Timestamp and datetime64

In [220]: x
Out[220]: numpy.datetime64('2012-06-17T23:00:05.453000000-0700')

In [221]: datetime.datetime.utcfromtimestamp(x.tolist()/1e9)
Out[221]: datetime.datetime(2012, 6, 18, 6, 0, 5, 452999)

Accounting for timezones I think that's right. Looks rather clunky though.

Using int() is more explicit (I think) than tolist()):

In [294]: datetime.datetime.utcfromtimestamp(int(x)/1e9)
Out[294]: datetime.datetime(2012, 6, 18, 6, 0, 5, 452999)

or to get datetime in local:

In [295]: datetime.datetime.fromtimestamp(x.astype('O')/1e9)

But in the test_datetime.py file https://github.com/numpy/numpy/blob/master/numpy/core/tests/test_datetime.py

I find some other options - first convert the general datetime64 to one of the format that specifies units:

In [296]: x.astype('M8[D]').astype('O')
Out[296]: datetime.date(2012, 6, 18)

In [297]: x.astype('M8[ms]').astype('O')
Out[297]: datetime.datetime(2012, 6, 18, 6, 0, 5, 453000)

This works for arrays:

In [303]: np.array([[x,x],[x,x]],dtype='M8[ms]').astype('O')[0,1]
Out[303]: datetime.datetime(2012, 6, 18, 6, 0, 5, 453000)
Sign up to request clarification or add additional context in comments.

3 Comments

So the long is the result of the units being [ns]
At first glance I thought that x.tolist()/1e9 was a mistake, because I didn't notice that x was a scalar in your case. I strongly suggest replacing it with x.item() to clarify.
22

Note that Timestamp IS a sub-class of datetime.datetime so the [4] will generally work

In [4]: pd.Timestamp(np.datetime64('2012-06-18T02:00:05.453000000-0400'))
Out[4]: Timestamp('2012-06-18 06:00:05.453000')

In [5]: pd.Timestamp(np.datetime64('2012-06-18T02:00:05.453000000-0400')).to_pydatetime()
Out[5]: datetime.datetime(2012, 6, 18, 6, 0, 5, 453000)

8 Comments

the bug Wes described was fixed quite a while ago
I meant that you've used the answer without attribution
oh I didn't realize he had one for this exact topic - so noted
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.