Creating integer timestamp column from date and time columns
Question:
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
Answers:
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.
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.)
I have not used pandas before but what about this?
from datetime import *
df['timestamp'] = int(datetime.fromisoformat(f"{df['date']} {df['time']}").timestamp())
If your column values are dates and times (not strings) then you can do this (I had to subtract 4 hours – 14400 seconds – because of the timezone):
time = [datetime(2022,8,4,21,11,7).time(), datetime(2022,8,4,21,11,7).time()]
date = [datetime(2022,8,4,0,0,0).date(), datetime(2022,8,4,0,0,0).date()]
df = pd.DataFrame({"time":time, "date":date})
df['timestamp']= df.apply(lambda r : int(datetime.combine(r['date'],r['time']).timestamp()-14400),1)
df
Output:
time date timestamp
0 21:11:07 2022-08-04 1659647467
1 21:11:07 2022-08-04 1659647467
Check the timestamp:
pd.to_datetime(1659647467,unit='s')
Timestamp('2022-08-04 21:11:07') ## output
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
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.
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.)
I have not used pandas before but what about this?
from datetime import *
df['timestamp'] = int(datetime.fromisoformat(f"{df['date']} {df['time']}").timestamp())
If your column values are dates and times (not strings) then you can do this (I had to subtract 4 hours – 14400 seconds – because of the timezone):
time = [datetime(2022,8,4,21,11,7).time(), datetime(2022,8,4,21,11,7).time()]
date = [datetime(2022,8,4,0,0,0).date(), datetime(2022,8,4,0,0,0).date()]
df = pd.DataFrame({"time":time, "date":date})
df['timestamp']= df.apply(lambda r : int(datetime.combine(r['date'],r['time']).timestamp()-14400),1)
df
Output:
time date timestamp
0 21:11:07 2022-08-04 1659647467
1 21:11:07 2022-08-04 1659647467
Check the timestamp:
pd.to_datetime(1659647467,unit='s')
Timestamp('2022-08-04 21:11:07') ## output