2

The following is a sample of my dataset:

index      time      speed
 0      00:00:00      15
 1      00:00:05      18
 2      00:00:10      23
 3      00:00:15      25
 4      00:00:20      34

I would like to create a for loop that does the same function as below:

for i in range (0,5,1):
    if df.speed[i] > df.speed [i+2]:
         print ('Larger')
    else:
         print('Smaller')

However, I would like to refer to time instead of indices in the FOR loop. For example:

for t in range (00:00:00, 00:00:20 , 5s):
  if df.speed[t] > df.speed [t+10s]: 
     print ('Larger')
  else:
     print('Smaller')

So the FOR LOOP will take the speed value at a certain t and compare with it the value of the speed after 10 seconds. If it is larger, then it prints Larger, otherwise, Smaller.

I appreciate any help. Thanks.

1 Answer 1

2

Date Range (Seconds)

It seems you're using pandas. If that's so, you really should look at it's Time Series features: https://pandas.pydata.org/pandas-docs/stable/timeseries.html

More specific, at pandas.date_range():

import pandas as pd

pd.date_range('00:00:00', '00:00:20', freq='5s')

Since it's a date range function, it'll return datetimes

DatetimeIndex(['2019-03-06 00:00:00',
               '2019-03-06 00:00:05',
                ...,
               '2019-03-06 00:00:20'], dtype='datetime64[ns]', freq='5S')

From here just use .strftime() to get what you want:

pd.date_range('00:00:00', '00:00:20', freq='5s').strftime('%H:%M:%S')

Returns

Index(['00:00:00', '00:00:05', ..., '00:00:20'], dtype='object')


Access dataframe by time

After that, in order to access your dataframe using this index you have two options:

  1. Using .loc to find the row where this time is:
for time in pd.date_range('00:00:00', '00:00:20', freq='5s').strftime('%H:%M:%S'):
    if (df.loc[df['time'] == time, 'speed'] ..):
  1. By redefining the dataframe index with set_index(), and then accessing it directly by time:
df.set_index('time', inplace=True)
for time in pd.date_range('00:00:00', '00:00:20', freq='5s').strftime('%H:%M:%S'):
    if (df.speed[time] ...):

Edit to address comment question

After you you use strftime() it is converted to a String. You could convert it back to add the 10 seconds like:

df.speed[(pd.to_datetime(time) + pd.to_timedelta(10, unit='s')).strftime('%H:%M:%S')].

Or:

for time in pd.date_range('00:00:00', '00:00:20', freq='5s'):
    if (df.speed[time.strftime('%H:%M:%S')]>df.speed[(time + pd.to_timedelta(10, unit='s')).strftime('%H:%M:%S')]):
1
  • Thanks for your answer. However, there is one more point I would like to know for the if condition how to add or remove seconds on the time. for example: if (df.speed[time]>df.speed[time+ 10]) or should it be (+00:00:10), or (+'10s')? Commented Mar 7, 2019 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.