Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert datetime object to milliseconds

I am parsing datetime values as follows:

df['actualDateTime'] = pd.to_datetime(df['actualDateTime'])

How can I convert this datetime objects to milliseconds?

I didn't see mention of milliseconds in the doc of to_datetime.

Update (Based on feedback): This is the current version of the code that provides error TypeError: Cannot convert input to Timestamp. The column Date3 must contain milliseconds (as a numeric equivalent of a datetime object).

import pandas as pd
import time

s1 = {'Date' : ['2015-10-20T07:21:00.000','2015-10-19T07:18:00.000','2015-10-19T07:15:00.000']}

df = pd.DataFrame(s1)

df['Date2'] = pd.to_datetime(df['Date'])

t = pd.Timestamp(df['Date2'])

df['Date3'] = time.mktime(t.timetuple())

print df
like image 383
Klausos Klausos Avatar asked Nov 02 '15 12:11

Klausos Klausos


2 Answers

Timestamps in pandas are always in nanoseconds.

This gives you milliseconds since the epoch (1970-01-01):

df['actualDateTime'] = df['actualDateTime'].astype(np.int64) / int(1e6)
like image 133
Mike Müller Avatar answered Sep 22 '22 17:09

Mike Müller


This will return milliseconds from epoch

timestamp_object.timestamp() * 1000

like image 31
Yannick Avatar answered Sep 22 '22 17:09

Yannick