All,
I have a pandas dataframe with a column (live.updated) containing a date and time combination as follows:
live live.updated live.latitude live.longitude live.altitude live.direction live.speed_horizontal live.speed_vertical live.is_ground
1 NaN 2022-10-11T17:45:54 00:00 -27.55 143.20 11277.6 139.0 853.772 0.0 False
2 NaN 2022-10-11T17:46:19 00:00 -45.35 169.88 5791.2 44.0 518.560 0.0 False
I would like to convert each element in the live.updated column into a timestamp (in seconds). I have tried applying the datetime module as described in https://stackabuse.com/converting-strings-to-datetime-in-python/, but this method does not seem fit with my specific case. Anyone know how to solve this such that I get a timestamp t = 12419933 seconds (for example)
CodePudding user response:
here is one way to do it
(((pd.to_datetime(df['live.updated']).dt.hour * 60) pd.to_datetime(df['live.updated']).dt.minute ) * 60
pd.to_datetime(df['live.updated']).dt.second)
OR
# extract time from the date, after converting ito datetime
tm=pd.to_datetime(df['live.updated']).dt.time
# get total_seconds using timedelta
pd.to_timedelta(tm.astype(str)).dt.total_seconds()
1 63954
2 63979
Name: live.updated, dtype: int64
CodePudding user response:
If I understand you right, you want to get unix timestamps out of the datetimes:
df["live.updated"] = pd.to_datetime(df["live.updated"]).astype(int) // 10 ** 9
print(df)
Prints:
live live.updated live.latitude live.longitude live.altitude live.direction live.speed_horizontal live.speed_vertical live.is_ground
1 NaN 1665510354 -27.55 143.20 11277.6 139.0 853.772 0.0 False
2 NaN 1665510379 -45.35 169.88 5791.2 44.0 518.560 0.0 False