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

独自空忆成欢 提交于 2019-12-04 12:14:24

问题


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?


回答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.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"]




回答2:


Try this:

import datetime as dt

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



回答3:


I don't believe working with datetime.time is recommended as you are trying to do. But one thing you can do to get around your problem is simply cast the datetime.time to a string, convert it to a pd.Timedelta and then do your addition. Like so:

print(df)
   Start_Time    Delta
0    00:00:00 00:00:00
1    01:00:00 01:00:00
2    02:00:00 02:00:00
3    03:00:00 03:00:00
4    04:00:00 04:00:00
5    05:00:00 05:00:00
6    06:00:00 06:00:00
7    07:00:00 07:00:00
8    08:00:00 08:00:00
9    09:00:00 09:00:00
10   10:00:00 10:00:00
11   11:00:00 11:00:00
12   12:00:00 12:00:00
13   13:00:00 13:00:00
14   14:00:00 14:00:00
15   15:00:00 15:00:00
16   16:00:00 16:00:00
17   17:00:00 17:00:00
18   18:00:00 18:00:00
19   19:00:00 19:00:00

df['End_Time'] = (pd.to_timedelta(df.Start_Time.astype(str)) + df.Delta).dt.components.hours
print(df)
   Start_Time    Delta  End_Time
0    00:00:00 00:00:00         0
1    01:00:00 01:00:00         2
2    02:00:00 02:00:00         4
3    03:00:00 03:00:00         6
4    04:00:00 04:00:00         8
5    05:00:00 05:00:00        10
6    06:00:00 06:00:00        12
7    07:00:00 07:00:00        14
8    08:00:00 08:00:00        16
9    09:00:00 09:00:00        18
10   10:00:00 10:00:00        20
11   11:00:00 11:00:00        22
12   12:00:00 12:00:00         0
13   13:00:00 13:00:00         2
14   14:00:00 14:00:00         4
15   15:00:00 15:00:00         6
16   16:00:00 16:00:00         8
17   17:00:00 17:00:00        10
18   18:00:00 18:00:00        12
19   19:00:00 19:00:00        14

Note that I accessed .dt.components.hours for the Timedelta series End_Time. If you do not do something like this you will end up also seeing the following as the Timdelta will keep track of the days as well.

   Start_Time    Delta        End_Time
0    00:00:00 00:00:00 0 days 00:00:00
1    01:00:00 01:00:00 0 days 02:00:00
2    02:00:00 02:00:00 0 days 04:00:00
3    03:00:00 03:00:00 0 days 06:00:00
4    04:00:00 04:00:00 0 days 08:00:00
5    05:00:00 05:00:00 0 days 10:00:00
6    06:00:00 06:00:00 0 days 12:00:00
7    07:00:00 07:00:00 0 days 14:00:00
8    08:00:00 08:00:00 0 days 16:00:00
9    09:00:00 09:00:00 0 days 18:00:00
10   10:00:00 10:00:00 0 days 20:00:00
11   11:00:00 11:00:00 0 days 22:00:00
12   12:00:00 12:00:00 1 days 00:00:00
13   13:00:00 13:00:00 1 days 02:00:00
14   14:00:00 14:00:00 1 days 04:00:00
15   15:00:00 15:00:00 1 days 06:00:00
16   16:00:00 16:00:00 1 days 08:00:00
17   17:00:00 17:00:00 1 days 10:00:00
18   18:00:00 18:00:00 1 days 12:00:00
19   19:00:00 19:00:00 1 days 14:00:00


来源:https://stackoverflow.com/questions/43506680/python-pandas-typeerror-unsupported-operand-types-for-datetime-time-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!