Skip to content

Instantly share code, notes, and snippets.

@ozturkib
Last active February 15, 2022 14:31
Show Gist options
  • Save ozturkib/331cec25082ba948c5d3cd22c67eafd0 to your computer and use it in GitHub Desktop.
Save ozturkib/331cec25082ba948c5d3cd22c67eafd0 to your computer and use it in GitHub Desktop.
# Take the difference of only the time portion of timestamps ignoring the date portion through dataframe columns.
import pandas as pd
import numpy as np
import datetime as dt
import datetime
from datetime import timedelta, datetime, date
df3 = pd.read_csv("combine.csv", parse_dates=['bl.start','bl.end','ex.start','ex.end','ExamStart','ScrathcpadStart'])
# extracting time from timestamp
df3['bl.start1'] = df3['bl.start'].dt.time
df3['ExamStart2'] = df3['ExamStart'].dt.time
#It has looping to subtract two time portions.
#Take into account that looping through dataframes with pandas is NOT efficient way!
#Dont have too much time to dive into your problem but have a look at timestamp, timedelta, timedate.
#So you need to make it better!
df3['diff_seconds'] = [datetime.combine(date.min, df3['bl.start1'][ind]) - datetime.combine(date.min, df3['ExamStart2'][ind]) for ind, row in df3.iterrows()]
df3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment