0

I have been searching for a way to replace itterrows with vectorization and coming up blank. I have this code which i believe to be working correctly using itterows which is taking forever.

sm_state = 0
fst_state = 0
time = 0
for index, row in df.iterrows():
    if row['Current'] < -5 or row['Current'] > 5 or row[active] == 0:
        sm_state = 0
        fst_state = 0
        time = 0
    else:
        sm_state = 1
        fst_state = 1
        time = row['ElapsedTime']

    if row['ElapsedTime'] - time >= smooth_time and sm_state == 1:
        sm_state = 2
    if row['ElapsedTime'] - time >= fast_time and fst_state == 1:
        fst_state = 2
    df.loc[index,'sm_timing'] = sm_state
    df.loc[index,'fst_timing'] = fst_state
return df

In summary what this is doing is when current is ~0 and active = 1 it stores the elapsedtime at that point (first time it is true) and new column should be a 1. Then if those conditions remain true for a "time" the value in the new column changes to a 2. if current goes outside of 0 the new column should return to a 0 and begin looking for current == 0 and active == 1 again.

any help or advice is greatly appreciated.

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
3ricVos
  • 1
  • 2
  • 1
    Welcome to Stackoverflow! This site is a great resource and you'll be able to leverage it more efficiently if you follow some simple guidelines. Please read [***MCVE***](http://stackoverflow.com/help/mcve), [***HowToAsk***](http://stackoverflow.com/help/how-to-ask), and [***GoodReproducableExamples***](http://stackoverflow.com/q/20109391/2336654) to get a better idea of what we expect. This is intended to help you get your question(s) answered. Hope that helps. – piRSquared Jun 11 '17 at 05:30
  • This code has inter-row dependencies and is not [easily] vectorizable. The only thing I can suggest is to use `row['Current'].abs() > 5` instead of your two conditional expressions. – DYZ Jun 11 '17 at 06:44
  • Thanks DYZ. I am using -5 and 5 for now but in the future they will be different threshold levels. I know it is not "easy", but can you think of any way to speed it up? – 3ricVos Jun 11 '17 at 17:11

0 Answers0