Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting between datetime, Timestamp and datetime64

How do I convert a numpy.datetime64 object to a datetime.datetime (or Timestamp)?

In the following code, I create a datetime, timestamp and datetime64 objects.

import datetime import numpy as np import pandas as pd dt = datetime.datetime(2012, 5, 1) # A strange way to extract a Timestamp object, there's surely a better way? ts = pd.DatetimeIndex([dt])[0] dt64 = np.datetime64(dt)  In [7]: dt Out[7]: datetime.datetime(2012, 5, 1, 0, 0)  In [8]: ts Out[8]: <Timestamp: 2012-05-01 00:00:00>  In [9]: dt64 Out[9]: numpy.datetime64('2012-05-01T01:00:00.000000+0100') 

Note: it's easy to get the datetime from the Timestamp:

In [10]: ts.to_datetime() Out[10]: datetime.datetime(2012, 5, 1, 0, 0) 

But how do we extract the datetime or Timestamp from a numpy.datetime64 (dt64)?

.

Update: a somewhat nasty example in my dataset (perhaps the motivating example) seems to be:

dt64 = numpy.datetime64('2002-06-28T01:00:00.000000000+0100') 

which should be datetime.datetime(2002, 6, 28, 1, 0), and not a long (!) (1025222400000000000L)...

like image 574
Andy Hayden Avatar asked Dec 04 '12 13:12

Andy Hayden


People also ask

Is datetime64 same as datetime?

NumPy's datetime64 and timedelta64 objectsThe datetime module's datetime object has microsecond precision (one-millionth of a second). NumPy's datetime64 object allows you to set its precision from hours all the way to attoseconds (10 ^ -18). It's constructor is more flexible and can take a variety of inputs.

What is datetime64 in Python?

New in version 1.7. 0. Starting in NumPy 1.7, there are core array data types which natively support datetime functionality. The data type is called datetime64 , so named because datetime is already taken by the Python standard library.


2 Answers

You can just use the pd.Timestamp constructor. The following diagram may be useful for this and related questions.

Conversions between time representations

like image 143
Quant Avatar answered Sep 27 '22 18:09

Quant


Welcome to hell.

You can just pass a datetime64 object to pandas.Timestamp:

In [16]: Timestamp(numpy.datetime64('2012-05-01T01:00:00.000000')) Out[16]: <Timestamp: 2012-05-01 01:00:00> 

I noticed that this doesn't work right though in NumPy 1.6.1:

numpy.datetime64('2012-05-01T01:00:00.000000+0100') 

Also, pandas.to_datetime can be used (this is off of the dev version, haven't checked v0.9.1):

In [24]: pandas.to_datetime('2012-05-01T01:00:00.000000+0100') Out[24]: datetime.datetime(2012, 5, 1, 1, 0, tzinfo=tzoffset(None, 3600)) 
like image 32
Wes McKinney Avatar answered Sep 27 '22 18:09

Wes McKinney