Shrads
Shrads

Reputation: 883

Calculate difference between consecutive rows within a same column

I am trying to calculate the difference between consecutive rows in the Timestamp column. I am getting the following error based on my piece of logic:

My log variable has data like below in it:

['Timestamp:', '1546626931.138813', 'ID:', '0764', 'S', 'DLC:', '8', 00', '00', '00', '00', '00', '00', '00', '00', 'Channel:', '0']
['Timestamp:', '1546626931.138954', 'ID:', '0365', 'S', 'DLC:', '8', 00', '00', '00', '80', 'db', '80', 'a2', '7f', 'Channel:', '1']
['Timestamp:', '1546626931.139053', 'ID:', '0765', 'S', 'DLC:', '8', '0d', '0f', '00', '00', 'fd', '0e', '00', '01', 'Channel:', '1']
['Timestamp:', '1546626931.139289', 'ID:', '0766', 'S', 'DLC:', '8', 'fd', '0e', '02', '01', 'fc', '0e', '03', '01', 'Channel:', '1']
.
.
.
.

The code is:

import can
import csv
import datetime
import pandas


filename = open('C:\\Users\\xyz\\Downloads\\BLF File\\output.csv', "w")
log = can.BLFReader('C:\\Users\\xyz\\Downloads\\BLF File\\test.blf')

log_output = []
timestamp = []                        #Variable to store timestamps from blf file
time_del = []                         #Variable to store time difference
print('We are here 1')
for time in log:
    time = str(time).split()
    timestamp.append(float(time[1]))
    # print(timestamp)

print("we are here 2")
count = 0

for i in range(len(timestamp)-1):
    delta_float= timestamp[count+1] - timestamp[count]
    count = count + 1
print(delta_float)

I am getting te following output:

We are here 1
we ar here 2
0.00022101402282714844
0.0002288818359375
0.00021910667419433594
0.00024199485778808594
.
.
.
.

why am i not getting the right difference in the delta_float? I should be getting something like below based on what values i have in the log variable,right?

0.141
0.99
0.236
.
.

Why this logic is not giving me the difference between the consecutive rows within the same column Timestamp?

Upvotes: 0

Views: 311

Answers (1)

You're only printing one value because you only have one print statement (not counting the "we are here" ones), it's not in a loop body, and it's printing a scalar value. You'd have to change at least one of those things, probably the second to do what you want, to get it to print multiple values.

Upvotes: 1

Related Questions