I am enhancing the speed of my script and saw the following answer: Iterrows Performance Issues. Here, the answer says that it is rarely needed to use iterrows.
In my code, I make use of iterrows since it is very straightforward and intuitive to use, however also very very slow. So I would like to vectorize the pieces of code I use iterrows. Here come two examples, where i cannot manage to find a solution. In both examples the values in the columns are all datetime values that have the following format: %Y-%m-%d %H:%M:%S
for index, row in df.iterrows():
df.loc[index, 'Time_Between']= row['Time_Begin'] + timedelta(seconds=row['Some_Integer_Seconds_In_A_Column'])
df.loc[index, 'Time_Required']= row['Time_End'] - timedelta(seconds=SomeIntegerSecondsAsAVariable)
df.loc[index, 'Tota_Time']= ((row['Time_Begin'] - row['Time_First']).total_seconds())/60
for index, row in df.iterrows():
if row['Time_Required'] > row['Time_Between']:
df.loc[index, 'Check']= 0
else:
df.loc[index, 'Check']= 1
How can I vectorized this? I tried masking and apply but I cannot get anything working. Most of the time I get: TypeError: Cannot change data-type for object array.
Something I dont get with the iterrows...