Reputation: 3729
I have a data frame with 2 column Date
and time
with type in string.
Date Time
20/11/2017 22:49:05
20/11/2017 22:51:07
20/11/2017 22:53:07
20/11/2017 22:58:07
I want to convert these two column into one column which is in seconds compare to 1970/01/01.
Anyone knows how to do that without install any packages but time
numpy
pandas
packages? Because I can not install package on my server which I don't have permission to do that, and the above three packages are pre-installed.
Thanks in advance.
Upvotes: 2
Views: 177
Reputation: 2939
You could try the following
df["datetime_column"] = (df["Date"] + " " + df["Time"]).to_datetime(infer_datetime_format=True)
(or you could specify the actual format if you aren't lazy like me)
df["seconds"] = df["datetime_column"].dt.strftime("%s").astype(int)
Upvotes: 1
Reputation: 164623
You can use the integer representation of a datetime
series:
# convert to datetime
df['DateTime'] = pd.to_datetime(df.pop('Date') + ' ' + df.pop('Time'))
# extract seconds
df['unix_seconds'] = df['DateTime'].astype(np.int64) // 10**9
print(df)
DateTime unix_seconds
0 2017-11-20 22:49:05 1511218145
1 2017-11-20 22:51:07 1511218267
2 2017-11-20 22:53:07 1511218387
3 2017-11-20 22:58:07 1511218687
Upvotes: 3