Python Pandas:类型错误:+ 不支持的操作数类型:'datetime.time' 和 'Timedelta'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43506680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Pandas: TypeError: unsupported operand type(s) for +: 'datetime.time' and 'Timedelta'
提问by DoctorWhom
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.
我试图在 Pandas 的数据框中添加两个系列,第一个系列是从 excel 文件导出的 24 小时时间值(例如 17:30),第二个系列是从 Timedelta 格式转换而来的相同长度的系列随 'pd.Timedelta' 命令浮动。
The desired resulting third column would be a 24-hr time regardless of day change (e.g. 22:00 + 4 hours = 02:00).
所需的结果第三列将是 24 小时时间,无论日变化如何(例如 22:00 + 4 小时 = 02:00)。
I created the Delta series like this:
我创建了这样的 Delta 系列:
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'
我收到的错误是: TypeError: unsupported operand type(s) for +: 'datetime.time' and 'Timedelta'
It seems this datetime.time format is immutable. Am I missing something?
似乎这种 datetime.time 格式是不可变的。我错过了什么吗?
回答by matusko
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
.
错误很明显。如果您检查元素的类型,您会发现在某些时候您要绑定添加datetime.time
object 和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
- python的内置
datetime
模块即datetime.time
,datetime.date
,datetime.timedelta
, ... - Pandas/麻木即
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
将所有内容转换为Pandas类型并最终提取时间
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
.
您应该确保,dtypes
您的列类似于datetime64[ns]
和timedelta64[ns]
。为此,请尝试使用pd.to_datetime
和显式转换它们pd.to_timedelta
。
Solution 2
解决方案2
Another approach would be just converting the Delta
column to datetime.timedelta
you could try
另一种方法是将Delta
列转换为datetime.timedelta
您可以尝试
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"]
但是您可能会遇到更多错误,具体取决于您df["Delta"]
和df["Start_Time"]
回答by zipa
Try this:
尝试这个:
import datetime as dt
df["end_Time"] = df["Start_Time"] + df["Delta"].map(dt.timedelta)