Convert a pandas column of int to timestamp datatype

14

4

I have a dataframe that among other things, contains a column of the number of milliseconds passed since 1970-1-1. I need to convert this column of ints to timestamp data, so I can then ultimately convert it to a column of datetime data by adding the timestamp column series to a series that consists entirely of datetime values for 1970-1-1.

I know how to convert a series of strings to datetime data (pandas.to_datetime), but I can't find or come up with any solution to convert the entire column of ints to datetime data OR to timestamp data.

Austin Capobianco

Posted 2016-10-19T21:22:43.257

Reputation: 433

Answers

18

You can specify the unit of a pandas to_datetime call.

Stolen from here:

# assuming `df` is your data frame and `date` is your column of timestamps

df['date'] = pandas.to_datetime(df['date'], unit='s')

Should work with integer datatypes, which makes sense if the unit is seconds since the epoch.

R Hill

Posted 2016-10-19T21:22:43.257

Reputation: 927

2what's the revers function? – Francesco Boi – 2019-01-22T15:51:41.660

@FrancescoBoi to_timedelta – Sai Krishnadas – 2020-09-29T06:00:44.110