I have a dataset with the following columns:
df=pd.read_csv('file.csv')
df
time date
0 21:11:07 2022-08-04
1 21:11:12 2022-08-04
2 21:11:27 2022-08-04
How do I get to this:
time date timestamp
0 21:11:07 2022-08-04 123238212
1 21:11:12 2022-08-04 123238217
2 21:11:27 2022-08-04 123238227
CodePudding user response:
I'll leave the pandas code to you, but you want the strptime function from datetime, this should get you there:
>>> from datetime import datetime
>>> time = '21:11:07'
>>> date = '2022-08-04'
>>> date_time_obj = datetime.strptime(f"{date} {time}", '%Y-%m-%d %H:%M:%S')
>>> print(date_time_obj.timestamp())
2022-08-04 21:11:07
(That pandas specific answer is probably better, but I'll leave this here.)
CodePudding user response:
I have not used pandas before but what about this?
from datetime import *
df['timestamp'] = int(datetime.fromisoformat(f"{df['date']} {df['time']}").timestamp())
CodePudding user response:
df['datetime'] = pd.to_datetime(df['date'] ' ' df['time'])
df['unix_timestamp'] = [i.timestamp() for i in df.datetime]
Convert date and time to datetime. Then get there timestamp.