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?
my code:
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
So here I have a summation equation to apply for X3 column value.
Then according to this summation equation I want to take the value of X3 in every hour.
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.
Then from that start time adding one hour one hour till to 6 hour I need to take the value for A.
For A equation is :
A = X3(5) - M
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)
Then got the values only 0 .
Then this error came .
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
In your code
time= data['duration'].eq(0)
this part is making thetime
variable of bool type. Try to convert it to0 or 1
then add it in for loop.If
time
is scalar (single) value :if
time
is vector (array) :Also, there is one more mistake in your for loop. You can't add
1
like this to data time. Try this :In this code you can replace
timedelta(hours=9)
by your timetimedelta(hours = time)
.Note : your
time
will be a binary array. you have to reaverse is to pick the values and add.