sudonym sudonym - 1 year ago 214
Python Question

How to convert a list of UNIX timestamps to a list of datetime objects in a pandas dataframe?

I have a pandas dataframe

with a column containing UNIX timestamps in the format

When i try to convert it with
df["Timestamp"] = pd.to_datetime(df["Timestamp"], unit = "s")

I am getting
OverflowError: Long too big to convert

What am I doing wrong?

Answer Source

You need unit='ms', maybe replace , to empty string:

df = pd.DataFrame({'Timestamp':['1,475,761,269,562', '1,475,761,269,562']})
print (df)
0  1,475,761,269,562
1  1,475,761,269,562

df["Timestamp"] = pd.to_datetime(df["Timestamp"].str.replace(',',''), unit = "ms")
print (df)
0 2016-10-06 13:41:09.562
1 2016-10-06 13:41:09.562
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download