Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am attempting to add two series in a dataframe in pandas with the first series being a 24-hr time value (e.g. 17:30) exported from an excel file and the second series being a series of the same length in Timedelta format converted from floats with the 'pd.Timedelta' command.

The desired resulting third column would be a 24-hr time regardless of day change (e.g. 22:00 + 4 hours = 02:00).

I created the Delta series like this:

delta = pd.Series(0 for x in range(0, len(df.Time_In_Hours)))

for j in range(0, len(df.Time_In_Hours)):
    delta[j] = pd.Timedelta(df.Time_In_Hours[j], 'h')
df = df.assign(Delta = delta)   
print ("Delta dtype = %s" % (df.Delta.dtype))
print ("Start_Time dtype = %s" % (df.Start_Time.dtype))

#Output
Delta dtype = object
Start_Time dtype = object

My goal is:

df["end_Time"] = df["Start_Time"] + df["Delta"]  

The error I am receiving is: TypeError: unsupported operand type(s) for +: 'datetime.time' and 'Timedelta'

It seems this datetime.time format is immutable. Am I missing something?

like image 960
DoctorWhom Avatar asked Apr 19 '17 21:04

DoctorWhom


People also ask

What is pd timedelta?

The to_timedelta() function is used to convert argument to datetime. Timedeltas are absolute differences in times, expressed in difference units (e.g. days, hours, minutes, seconds). This method converts an argument from a recognized timedelta format / value into a Timedelta type.

How do I combine date and time columns in pandas?

Pandas Combine() Function combine() function which allows us to take a date and time string values and combine them to a single Pandas timestamp object. The function accepts two main parameters: Date – refers to the datetime. date object denoting the date string.

How do I change timestamp to DateTime?

Timestamp to DateTime object You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.


2 Answers

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.time, datetime.date, datetime.timedelta, ...
  • pandas / numpy i.e pandas.Timestamp, pandas.Timedelta

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

Solution 1

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.

Solution 2

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"]

like image 149
matusko Avatar answered Oct 05 '22 03:10

matusko


Try this:

import datetime as dt

df["end_Time"] = df["Start_Time"] + df["Delta"].map(dt.timedelta)
like image 26
zipa Avatar answered Oct 05 '22 05:10

zipa