BD12
BD12

Reputation: 107

Comparing timedelta fields

I am looking at file delivery times and can't work out how to compare two timedelta fields using a for loop if statement.

time_diff is the difference between cob_date and last_update_time

average_diff is based on the average for a particular file

I want to find the delay for each row.

I have been able to produce a column delay using average_diff - time_diff

However, when the average_diff - time_diff < 0 I just want to return delay = 0 as this is not a delay.

I have made a for loop but this isn't working and I don't know why. I'm sure the answer is very simple but I can't get there.

test_pv_import_v2['delay2'] = pd.to_timedelta('0')

for index, row in test_pv_import_v2.iterrows():
    if test_pv_import_v2['time_diff'] > test_pv_import_v2['average_diff'] :
        test_pv_import_v2['delay2'] = test_pv_import_v2['time_diff'] - test_pv_import_v2['average_diff']

enter image description here

Upvotes: 1

Views: 32

Answers (1)

jezrael
jezrael

Reputation: 862406

Use Series.where for set 0 Timedelta by condition:

mask = test_pv_import_v2['time_diff'] > test_pv_import_v2['average_diff']
s = (test_pv_import_v2['time_diff'] - test_pv_import_v2['average_diff'])

test_pv_import_v2['delay2'] = s.where(mask, pd.to_timedelta('0'))

Upvotes: 1

Related Questions