1

I got a dataframe with one column containing the date-time data with milli seconds precision. How can I convert it to unix timestamp ?

My data is of the form

0         2017-05-16 00:00:00.008
1         2017-05-16 00:00:00.272
2         2017-05-16 00:00:01.551
3         2017-05-16 00:00:01.813
4         2017-05-16 00:00:03.091
1

1 Answer 1

1

You can use the following to convert each entry into a timestamp:

from datetime import datetime

t = '2017-05-16 00:00:00.008'
ts = datetime.strptime(t, "%Y-%m-%d %H:%M:%S.%f").timestamp()

print(ts)  # 1494885600.008

if your column name is d then you can convert each value into a unix timestamp as follows:

df['d'] = df['d'].apply(lambda t: datetime.strptime(t, "%Y-%m-%d %H:%M:%S.%f").timestamp())
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.