0

I am trying to calculate machine downtime in a python code which is predominantly pandas dataframe manipulation. If a repair number starts during any of the days then I have take take time bewteen start of the day and start of the work order as my machine available time. I have created the column for day start time as in every row which is at a day level granularity so that when there is a repair order I just need to subtract two time fields to get my answer in seconds/hours. But I am currently getting error as 'Timedelta' object has no attribute 'astype'. I tried out of a few options suggested here but to no vail. I know I am doing somehting very fundamentally wrong, please point me in the right directions.

for i, row in df4.iterrows():
    if (row['Date'] == row['START_DATE']) | (row['Date'] == row['END_DATE']):
        if (row['START_DATE'] == row['Date']) & (row['END_DATE'] != row['Date']):

            value = pd.to_timedelta(row.REPAIR_START-row.Date_start).astype('timedelta64[h]')
            df4.set_value(i,'avail',value)

        if (row['START_DATE'] != row['Date']) & (row['END_DATE'] == row['Date']):

            value = pd.to_timedelta((row.REPAIR_FINISH-row.Date_end)).astype('timedelta64[h]')
            df4.set_value(i,'avail',value)

        if (row['START_DATE'] == row['Date']) & (row['END_DATE'] == row['Date']):
            value = pd.to_timedelta((row.REPAIR_START-row.REPAIR_FINISH)).astype('timedelta64[h]')
            df4.set_value(i,'avail',value)
        else: df4.set_value(i,'avail',full_day)

Data is at Machine , Date level. Explanation of the metadata of the code:
Date : Daily date
START_DATE : Only date part of the repair timestamp
END_DATE : Only date part of the repair timestamp
REPAIR_START : Repair order start timestamp
REPAIR_START : Repair order end timestamp

Valentino
  • 7,291
  • 6
  • 18
  • 34
  • By just giving : value = (row.REPAIR_FINISH-row.Date_end) ; I am able to get a time value : 0 days 11:59:59 but i need it in hours or seconds . – Suhas Rajashekar May 27 '19 at 10:13
  • value is of type timedelta, it has a method called total_seconds() it returns it in seconds. value = (row.REPAIR_FINISH-row.Date_end).total_seconds() – ichafai May 27 '19 at 10:45
  • It is working !! Much Obliged. – Suhas Rajashekar May 27 '19 at 12:00
  • Great, you're welcome ! – ichafai May 27 '19 at 13:48
  • Does this answer your question? [Calculate Pandas DataFrame Time Difference Between Two Columns in Hours and Minutes](https://stackoverflow.com/questions/22923775/calculate-pandas-dataframe-time-difference-between-two-columns-in-hours-and-minu) – Trenton McKinney Aug 23 '20 at 01:28

0 Answers0