+不支持的操作数类型:"float"和"datetime.timedelta",“出现在索引5") [英] unsupported operand type(s) for +: 'float' and 'datetime.timedelta'", 'occurred at index 5')
问题描述
我有一个带有日期和时间输入的数据集.
首先,我编写了代码以在X3列中找到5的第一个时间值,然后将该时间变为0.
然后,我尝试将timedelta(hours = 1)添加到范围为6的范围中.
然后它给了我这个错误"unsupported operand type(s) for +: 'bool' and 'datetime.timedelta'"
I have a dataset with one input with date and time .
First I wrote the code to find the first time of 5 value in X3 column and I turn that time into 0.
Then I tried to add timedelta(hours=1) into that which is having range 6.
Then it gave me this error "unsupported operand type(s) for +: 'bool' and 'datetime.timedelta'"
有人可以帮助我解决此错误吗?
Can anyone help me to solve this error?
我的代码:
data =pd.read_csv('data6.csv')
data['time_diff']= pd.to_datetime(data['date'] + " " + data['time'],
format='%d/%m/%Y %H:%M:%S', dayfirst=True)
mask = data['X3'].eq(5)
data['duration'] = data[mask].drop_duplicates(['date','X3']).groupby(['date','X3'])['time_diff'].transform('first')
data['duration'] = data['time_diff'].sub(data['duration']).dt.total_seconds().div(3600)
date time x3 Time(expected)
10/3/2018 6:15:00 0 NaN
10/3/2018 6:45:00 5 0.0
10/3/2018 7:45:00 0 NaN
10/3/2018 9:00:00 0 NaN
10/3/2018 9:25:00 7 NaN
10/3/2018 9:30:00 0 NaN
10/3/2018 11:00:00 0 NaN
10/3/2018 11:30:00 0 NaN
10/3/2018 13:30:00 0 NaN
10/3/2018 13:50:00 5 NaN
10/3/2018 15:00:00 0 NaN
10/3/2018 15:25:00 0 NaN
10/3/2018 16:25:00 0 NaN
10/3/2018 18:00:00 7 NaN
10/3/2018 19:00:00 0 NaN
10/3/2018 19:30:00 0 NaN
10/3/2018 20:00:00 0 NaN
10/3/2018 22:05:00 0 NaN
10/3/2018 22:15:00 5 NaN
10/3/2018 23:40:00 0 NaN
10/4/2018 6:58:00 5 0.0
10/4/2018 13:00:00 0 NaN
10/4/2018 16:00:00 7 NaN
10/4/2018 17:00:00 7 NaN
所以在这里,我有一个求和公式可用于X3列值.
So here I have a summation equation to apply for X3 column value.
然后根据这个求和方程,我想每小时获取X3的值.
Then according to this summation equation I want to take the value of X3 in every hour.
这就是为什么我首先找到每天提及值5的开始时间,然后将该时间转换为0:00:00的原因.
That's why first I found the start time of mention value of 5 in every day and then convert that time into 0:00:00.
然后从开始时间开始,将一小时一小时加到六小时,直到我需要取A的值.
Then from that start time adding one hour one hour till to 6 hour I need to take the value for A.
对于一个等式,是:
A = X3(5) - M
所以我首先分别进行第一次,只有0次. 为此,我使用了代码:
So first I took the first time separately, 0 time only. For that I used the code :
time= data['duration'].eq(0)
然后我在课堂上写了这个等式方法
Then I wrote this equation method inside the class
time=0
M=0
for _ in range(len(data['X3'])):
if X3.all()==5:
if time ==data['duration'].eq(5).all():
M=X3
for i in (time + timedelta(hours=1*it) for it in range(6)):
M = 5 - 0.0015 * np.sum(i*X3)
print(M)
然后只有0的值.
然后出现此错误.
从这些代码中,我期望的输出是:
From these code what I am expecting output is:
time expected output
0 (start time of x3 value of 5) 5
1 hr 5-0.3(according to the summation equation) = 4.7
2hr 5-0.6=4.4
3hr 5-0.9=4.1
4hr 5-1.2=3.8
5hr 5-1.5=3.5
6hr 5-1.8=3.2
推荐答案
在您的代码time= data['duration'].eq(0)
中,此部分将time
变量设为bool类型.
尝试将其转换为0 or 1
,然后将其添加到for循环中.
In your code time= data['duration'].eq(0)
this part is making the time
variable of bool type.
Try to convert it to 0 or 1
then add it in for loop.
如果time
是标量(单个)值:
If time
is scalar (single) value :
if (time==True):
time=1
else:
time=0
如果time
是向量(数组):
time_array = [0 if tm==False else 1 for tm in data['duration'].eq(0)]
此外,您的for循环中还有另外一个错误.您不能将这样的1
添加到数据时间.
试试这个:
Also, there is one more mistake in your for loop. You can't add 1
like this to data time.
Try this :
from datetime import datetime, timedelta
current_time = datetime.now()
nine_hours_from_now = current_time + timedelta(hours=9)
在此代码中,您可以在时间timedelta(hours = time)
之前替换timedelta(hours=9)
.
In this code you can replace timedelta(hours=9)
by your time timedelta(hours = time)
.
注意:您的time
将是一个二进制数组.您必须考虑的是选择值并添加.
Note : your time
will be a binary array. you have to reaverse is to pick the values and add.
这篇关于+不支持的操作数类型:"float"和"datetime.timedelta",“出现在索引5")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!