User12345
User12345

Reputation: 5480

Column from date time to Unix time-stamp in pandas

I have a data frame in pandas with a single column called Time_column.

This column is of datetime64 datatype. Now I want to convert this column to unix timestamp

           Time_column
0  2017-03-12 02:43:52
1  2017-03-12 02:56:32
2  2017-03-12 02:56:32
3  2017-03-12 03:16:23
4  2017-03-12 03:17:15
5  2017-03-12 03:22:19
6  2017-03-12 03:22:19
7  2017-03-12 03:33:07
8  2017-03-12 03:33:07
9  2017-03-12 03:43:16
10 2017-03-12 03:43:16

How can I do that?

I have tried like below

import datetime
t = datetime.datetime(2018, 01, 30, 0, 0)
(t-datetime.datetime(1970,1,1)).total_seconds()
1517270400.0

I am able to do this for one record. How can I apply this for a column?

Upvotes: 1

Views: 3023

Answers (1)

BENY
BENY

Reputation: 323306

With apply

#df.Time_column=pd.to_datetime(df.Time_column)

df.Time_column.apply(lambda x : (x-datetime.datetime(1970,1,1)).total_seconds())

Upvotes: 1

Related Questions