Marc Schwambach
Marc Schwambach

Reputation: 438

Access past datetime in a for loop

I am currently working on a problem and now got stuck to implement one of it's steps. I made up the following example which is a simple scenario in order to explore possibilities. The length of the text might be scary but it was only my attempt to better explain it.

What is going on is that I have a for loop for each second, I have the Variable_1, which is a reference value that is utilized to set the Variable_2. What happens on this code is that for each second, it compares the Variable_1[i] with the Variable_2i, if the difference is bigger than 1.5, then, the Variable_2i is updated. In the end, the results are stored at Variable_2.

import pandas as pd
import numpy as np

df = pd.DataFrame()
start = pd.Timestamp('2013-08-14T00:00')
end = pd.Timestamp('2013-08-14T01:00')
t = np.linspace(start.value, end.value, 60*60+1)
df['Timestamp'] = pd.to_datetime(t)
df['Variable_1'] = 270 +  90*np.sin(2*np.pi*1*np.arange(3601)/3601)

Variable_2i = 270
Variable_2 = np.array([])

for i,val in enumerate(df['Variable_1'].values):    
    if abs(val - Variable_2i) >= 1.5:
        Variable_2i = val
    else:    
        Variable_2i = Variable_2i

    Variable_2 = np.append(Variable_2, Variable_2i)

df['Variable_2'] = Variable_2

However, suppose that now I want this of Variable_2i to be updated with a rate of 0.3 units per second until it reaches the value for which the condition is satisfied. That means that instead of expecting the result which I get that it is something similar as this:

               Timestamp  Variable_1  Variable_2
0    2013-08-14 00:00:00  270.000000  270.000000
1    2013-08-14 00:00:01  270.157036  270.000000
2    2013-08-14 00:00:02  270.314071  270.000000
3    2013-08-14 00:00:03  270.471106  270.000000
4    2013-08-14 00:00:04  270.628139  270.000000
5    2013-08-14 00:00:05  270.785170  270.000000
6    2013-08-14 00:00:06  270.942199  270.000000
7    2013-08-14 00:00:07  271.099225  270.000000
8    2013-08-14 00:00:08  271.256247  270.000000
9    2013-08-14 00:00:09  271.413266  270.000000
10   2013-08-14 00:00:10  271.570280  271.570280
11   2013-08-14 00:00:11  271.727290  271.570280
12   2013-08-14 00:00:12  271.884294  271.570280
13   2013-08-14 00:00:13  272.041293  271.570280
14   2013-08-14 00:00:14  272.198286  271.570280
15   2013-08-14 00:00:15  272.355271  271.570280

I would get something as this:

               Timestamp  Variable_1  Variable_2
0    2013-08-14 00:00:00  270.000000  270.000000
1    2013-08-14 00:00:01  270.157036  270.000000
2    2013-08-14 00:00:02  270.314071  270.000000
3    2013-08-14 00:00:03  270.471106  270.000000
4    2013-08-14 00:00:04  270.628139  270.000000
5    2013-08-14 00:00:05  270.785170  270.000000
6    2013-08-14 00:00:06  270.942199  270.000000
7    2013-08-14 00:00:07  271.099225  270.000000
8    2013-08-14 00:00:08  271.256247  270.000000
9    2013-08-14 00:00:09  271.413266  270.000000
10   2013-08-14 00:00:10  271.570280  270.300000
11   2013-08-14 00:00:11  271.727290  270.600000
12   2013-08-14 00:00:12  271.884294  270.900000
13   2013-08-14 00:00:13  272.041293  271.200000
14   2013-08-14 00:00:14  272.198286  271.500000
15   2013-08-14 00:00:15  272.355271  271.570280

Note the difference on the last values of the column Variable_2 to better understand what I mean. From what I could think by myself so far, there must be a way to keep the reference value without being update or access the past datetime in a for loop, however as I am quite new to python, I am not sure if such think exists.

Hope that I managed to be succinct and precise. I would really appreciate your help on this one!

Upvotes: 1

Views: 64

Answers (1)

Kieran Moynihan
Kieran Moynihan

Reputation: 593

Hopefully I'm understanding what you're looking for here.

Declare a target variable outside your for-loop. This will hold the value that is going to be moved towards in steps of 0.3:

target = Variable_2i

Set a target whenever you reach your specified threshold of the val and Variable_2i have a difference greater than or equal to 1.5:

if abs(val - Variable_2i) >= 1.5:
    target = val

When you're looping through, if the target is greater than your Variable_2i, you want to step towards it by 0.3:

if target != Variable_2i:
    separation = target - Variable_2i
    if abs(separation) > 0.3:
        Variable_2i += 0.3 if separation > 0 else -0.3
    else:
        Variable_2i = target

Putting this all togther:

Variable_2i = 270
target = Variable_2i
Variable_2 = np.array([])

for val in df['Variable_1'].values:
    if target != Variable_2i:
        separation = target - Variable_2i
        if abs(separation) > 0.3:
            Variable_2i += 0.3 if separation > 0 else -0.3
        else:
            Variable_2i = target
    elif abs(val - Variable_2i) >= 1.5:
        target = val

    Variable_2 = np.append(Variable_2, Variable_2i)

df['Variable_2'] = Variable_2

Note: This will step towards whatever triggered the change in the first place, until it gets there, even if the real value swings back in the other direction.

Upvotes: 1

Related Questions