Search code examples
pythonpandasdatetimetimestampunix-timestamp

Converting to UNIX time


I have a .csv file that contains:

created_at actual_delivery_time
2015-02-06 22:24:17 2015-02-06 23:27:16
2015-02-10 21:49:25 2015-02-10 22:56:29

I want to convert these columns from datetime to UNIX timestamp.

For created_at, I was able to convert:

ndf["created_unix"] = pd.to_datetime(ndf["created_at"])
ndf["created_unix"] = (ndf["created_unix"] - pd.Timestamp("1970-01-01")) // pd.Timedelta("1s")

Which outputted:

created_unix
1423261457
1423604965

This is exactly what I'm looking for, however following the same steps for column actual_delivery_time produces a different result:

ndf["actual_unix"] = pd.to_datetime(ndf["actual_delivery_time"])
ndf["actual_unix"] = (ndf["actual_unix"] - pd.Timestamp("1970-01-01")) // pd.Timedelta("1s")

This outputs:

actual_unix
1.423265e+09
1.423609e+09

It is readable by the Epoch converter, but it isn't the correct timestamp. Is there something I'm doing wrong?

I'm not sure why its producing a different result.


Solution

  • I'm not sure why its producing a different result.

    Consult ndf.dtypes.

    You are complaining that one column is of type int, while the other is a float.

    Likely one or more actual_delivery_time values were blank, and pandas represented that with a floating-point NaN.