close

[Solved] Python Pandas: TypeError: unsupported operand type(s) for +: ‘datetime.time’ and ‘Timedelta’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python Pandas: TypeError: unsupported operand type(s) for +: ‘datetime.time’ and ‘Timedelta’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python Pandas: TypeError: unsupported operand type(s) for +: ‘datetime.time’ and ‘Timedelta’ Error Occurs?

Today I get the following error Python Pandas: TypeError: unsupported operand type(s) for +: ‘datetime.time’ and ‘Timedelta’ in python.

How To Solve Python Pandas: TypeError: unsupported operand type(s) for +: ‘datetime.time’ and ‘Timedelta’ Error ?

  1. How To Solve Python Pandas: TypeError: unsupported operand type(s) for +: 'datetime.time' and 'Timedelta' Error ?

    To Solve Python Pandas: TypeError: unsupported operand type(s) for +: 'datetime.time' and 'Timedelta' Error The error is pretty clear. If you check the types of the elements, you will find out that at some point you are tying to add datetime.time object and pandas.Timedelta.

  2. Python Pandas: TypeError: unsupported operand type(s) for +: 'datetime.time' and 'Timedelta'

    To Solve Python Pandas: TypeError: unsupported operand type(s) for +: 'datetime.time' and 'Timedelta' Error The error is pretty clear. If you check the types of the elements, you will find out that at some point you are tying to add datetime.time object and pandas.Timedelta.

Solution 1

The cause

The error is pretty clear. If you check the types of the elements, you will find out that at some point you are tying to add datetime.time object and pandas.Timedelta.

There are 2 kinds of dates, times and timedeltas:

  • python’s builtin from datetime module i.e. datetime.timedatetime.datedatetime.timedelta, …
  • pandas / numpy i.e pandas.Timestamppandas.Timedelta

these two stacks are incompatible for basic operations as addition or comparison.

Convert everything to pandas type and extract the times in the end

You should make sure, that dtypes of your columns are something like datetime64[ns] and timedelta64[ns]. For that, try converting them explicitly using pd.to_datetime and pd.to_timedelta.

Another approach would be just converting the Delta column to datetime.timedelta you could try

df["end_Time"] = df["Start_Time"] + df["Delta"].map(pd.Timedelta.to_pytimedelta)

But you may run into some more errors depending on what is in your df["Delta"] and df["Start_Time"]

Solution 2

Try this:

import datetime as dt

df["end_Time"] = df["Start_Time"] + df["Delta"].map(dt.timedelta)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read