2

Is it possible to convert something like this;

array([datetime.datetime(2014, 2, 1, 0, 0, 0, 100000),
       datetime.datetime(2014, 2, 1, 0, 0, 0, 300000),
       datetime.datetime(2014, 2, 1, 0, 0, 0, 500000), ...,
       datetime.datetime(2014, 2, 1, 19, 30, 0, 500000),
       datetime.datetime(2014, 2, 1, 19, 30, 0, 700000),
       datetime.datetime(2014, 2, 1, 19, 30, 0, 900000)], dtype=object)

to this:

array([  1.39277301e+09,   1.39277301e+09,   1.39277301e+09, ...,
         1.39285442e+09,   1.39285442e+09,   1.39285442e+09])

I would basically like to convert the datetime.datetime into a timestamp, with dtype = float.

2
  • 1
    What's the rule or mapping you want to use for converting datetimes to floats? i.e. why should datetime.datetime(2014, 2, 1, 0, 0, 0, 100000) become 1.39277301e+09? Commented Apr 25, 2017 at 5:01
  • Look at using np.datetime64 Commented Apr 25, 2017 at 5:15

4 Answers 4

5

The best way to get a timestamp is by subtracting epoch from your datetime as:

Code:

import datetime as dt

times = np.array([
    dt.datetime(2014, 2, 1, 0, 0, 0, 100000),
    dt.datetime(2014, 2, 1, 0, 0, 0, 300000),
    dt.datetime(2014, 2, 1, 0, 0, 0, 500000),
])

# get a datetime that is equal to epoch
epoch = dt.datetime(1970, 1, 1)

for t in [(d - epoch).total_seconds() for d in times]:
    print('%.6f' % t)

Results:

1391212800.100000
1391212800.300000
1391212800.500000
Sign up to request clarification or add additional context in comments.

Comments

1

How about something like this:

(Python 2.x):

import datetime

temp_array = numpy.array([(i - datetime.datetime(1970, 1, 1)).total_seconds() for i in old_array])
new_array = temp_array.astype('float')

For Python 3.x:

import datetime

temp_array = numpy.array([(i - datetime.datetime(1970, 1, 1)) / datetime.timedelta(seconds=1) for i in old_array])
new_array = temp_array.astype('float')

Comments

1

mktime() will convert into a timestamp, but it seems to lose accuracy beyond seconds.

>>> import datetime
>>> from time import mktime

>>> x = datetime.datetime.now()
>>> y = mktime(x.timetuple())
>>> print(y)
1493096455.0

>>> type(y)
<type 'float'>

>>> z = y/1000000000
>>> print(z)
1.493096455

Comments

0
In [475]: x
Out[475]: 
array([datetime.datetime(2014, 2, 1, 0, 0, 0, 100000),
       datetime.datetime(2014, 2, 1, 0, 0, 0, 300000),
       datetime.datetime(2014, 2, 1, 0, 0, 0, 500000),
       datetime.datetime(2014, 2, 1, 19, 30, 0, 500000),
       datetime.datetime(2014, 2, 1, 19, 30, 0, 700000),
       datetime.datetime(2014, 2, 1, 19, 30, 0, 900000)], dtype=object)
In [476]: x.astype('datetime64[us]')
Out[476]: 
array(['2014-02-01T00:00:00.100000', '2014-02-01T00:00:00.300000',
       '2014-02-01T00:00:00.500000', '2014-02-01T19:30:00.500000',
       '2014-02-01T19:30:00.700000', '2014-02-01T19:30:00.900000'], dtype='datetime64[us]')

Each is saved as an 8 byte float which is interpreted as a datetime.

In [477]: _.dtype
Out[477]: dtype('<M8[us]')
In [478]: __.itemsize
Out[478]: 8

You can change the units (year, day, etc); do math, etc

In [479]: x1=x.astype('datetime64[us]')
In [480]: np.diff(x1)
Out[480]: array([     200000,      200000, 70200000000,      200000,      200000], dtype='timedelta64[us]')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.