Skip to content Skip to sidebar Skip to footer

Unsupported Operand Type(s) For -: 'datetime.time' And 'datetime.time'

Ca Tên NVNL Check in Check out Thời gian làm việc trong ca Hỗ trợ ăn trưa 0 Ca Sáng Ngô Hải Anh 08:15:00 12:13:00 NaN NaN 1 Ca Ch

Solution 1:

I think here is possible convert values to timedeltas by to_timedelta, but first cast to strings:

data['Thời gian làm việc']= (pd.to_timedelta(data['Check out'].astype(str)) - 
                             pd.to_timedelta(data['Check in'].astype(str)))

Or to datetimes by to_datetime:

data['Thời gian làm việc']= (pd.to_datetime(data['Check out'].astype(str)) - 
                             pd.to_datetime(data['Check in'].astype(str)))

Solution 2:

I'd suggest you take a step back and think about your data model. A time is not a point in time, quoting from Python's documentation:

A time object represents a (local) time of day, independent of any particular day

.. so substracting times doesn't make a lot of sense. Say I check in at 9pm and check out two days later at 8pm. What would you expect "8pm minus 9pm" to represent?

Your check in / check out columns would probably be better modeled as datetime which do represent points in time. Subtracting datetimes does work, and it has an actual meaning: the time which separates the two points in time.

Post a Comment for "Unsupported Operand Type(s) For -: 'datetime.time' And 'datetime.time'"