0

I'm using iterrows in a dataframe like below to get the time difference

       ImageA_Time            ImageB_Time
0      2022-02-01 06:56:22    2022-02-02 06:56:22
1      No time info           2022-02-02 06:57:22
2      No time info           2022-02-02 06:57:22

I want to add a column called 'Time Delta' showing the time difference of ImageA and ImageB for each row. Here is the desired output:

       ImageA_Time            ImageB_Time             Time_Delta
0      2022-02-01 06:56:22    2022-02-02 06:56:22     24h
1      No time info           2022-02-02 06:57:22     Nan
2      No time info           2022-02-02 06:57:22     Nan

My question is how to write if sentence here when doing iterrow? Below is my idea but it still shows

TypeError: unsupported operand type(s) for -: 'str' and 'Timestamp'

for index,row in df.iterrows():
    if row['ImageA_Time'] is str:
        df['Time_Delta'] = 'Nan'
    else:
        df['Time_Delta'] = row['ImageA_Time'] - row['ImageB_Time']
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Elaine Yang
  • 113
  • 7

0 Answers0