mardi 27 juillet 2021

Python: Pairwise computation of acceleration using for loop

Here's a sample dataset with observations from 4 different trips (there are 4 unique trip IDs):

    trip_id                                time_interval    speed
0   8a8449635c10cc4b8e7841e517f27e2652c57ea3    873.96  0.062410
1   8a8449635c10cc4b8e7841e517f27e2652c57ea3    11.46   0.000000
2   8a8449635c10cc4b8e7841e517f27e2652c57ea3    903.96  0.247515
3   8a8449635c10cc4b8e7841e517f27e2652c57ea3    882.48  0.121376
4   8a8449635c10cc4b8e7841e517f27e2652c57ea3    918.78  0.185405
5   8a8449635c10cc4b8e7841e517f27e2652c57ea3    885.96  0.122147
6   f7fd70a8c14e43d8be91ef180e297d7195bbe9b0    276.60  0.583178
7   84d14618dcb30c28520cb679e867593c1d29213e    903.48  0.193313
8   84d14618dcb30c28520cb679e867593c1d29213e    899.34  0.085377
9   84d14618dcb30c28520cb679e867593c1d29213e    893.46  0.092259
10  84d14618dcb30c28520cb679e867593c1d29213e    849.36  0.350341
11  3db35f9835db3fe550de194b55b3a90a6c1ecb97    10.86   0.000000
12  3db35f9835db3fe550de194b55b3a90a6c1ecb97    919.50  0.071119

I am trying to compute the acceleration of each unique trip from one point to another.

Example:

  • first acceleration value will be computed using rows 0 and 1 (0 initial value; 1 final value)
  • second acceleration value will be computed using rows 1 and 2 (1 initial value; 2 final value)
  • ... and so on.

As I want to compute this for each individual trip based on trip_id, this is what I attempted:

def get_acceleration(dataset):
    
    ##### INITIALISATION VARIABLES #####
    # Empty string for the trip ID
    current_trip_id = ""
    
    # Copy of the dataframe
    dataset2 = dataset.copy()
    
    # Creating a new column for the acceleration between observations of the same trip
    # all rows have a default value of 0
    dataset2["acceleration"] = 0
    
    ##### LOOP #####
    for index,row in dataset.iterrows():
        
        # Checking if we are looking at the same trip
        # when looking at the same trip, the default values of zero are replaced
        # by the calculated trip characteristic
        if row["trip_id"] == current_trip_id:
            
            # Final speed and time
            final_speed = row["speed"]
            final_time = row["time_interval"]
            
            print(type(final_speed))
                      
            # Computing the acceleration (delta_v/ delta_t)
            acceleration = (final_speed[1] - initial_speed[0])/(final_time[1] - initial_time[0])
            
            # Adding the output to the acceleration column
            dataset2.loc[index, "acceleration"] = acceleration
        
        ##### UPDATING THE LOOP #####
        current_trip_id = row["trip_id"]
        
        # Initial speed and time
        initial_speed = row["speed"]
        initial_time = row["time_interval"]
        
    return dataset2

However, I get the error:


<ipython-input-42-0255a952850b> in get_acceleration(dataset)
     27 
     28             # Computing the acceleration (delta_v/ delta_t)
---> 29             acceleration = (final_speed[1] - initial_speed[0])/(final_time[1] - initial_time[0])
     30 
     31             # Adding the output to the acceleration column

TypeError: 'float' object is not subscriptable

How could I fix this error and compute the acceleration?

UPDATE:

After using the answer below, to avoid division by zero just add an if and else statements.

            delta_speed = final_speed - initial_speed
            delta_time = final_time - initial_time
            
            # Computing the acceleration (delta_v/ delta_t)
            if delta_time != 0:
                acceleration = (delta_speed)/(delta_time)
            
            else:
                acceleration = 0

Aucun commentaire:

Enregistrer un commentaire