Search code examples
pythondatetimepandasunix-timestamp

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


I have a pandas dataframe df with a column containing UNIX timestamps in the format 1,475,761,269,562.

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?


Solution

  • 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)
               Timestamp
    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)
                    Timestamp
    0 2016-10-06 13:41:09.562
    1 2016-10-06 13:41:09.562