I am running a hydrologic model in Python which requires iterative routing calculations. In this methodology, the output from one row in the dataframe must be used as an input to the next row. This is accomplished via a series of conditional statements which reference dataframe cells, getting values from previous rows and setting values in the current row. These statements correctly set cell values when I use df.loc[] as the setter, however, they do not set cell values when I use df.iat[] as the setter. In both cases, the calculated value is correct, it's just a matter of actually setting that value.
Is there a known reason or set of conditions .iat[] wouldn't work as a setter? According to Python documentation and this question, it seems like .iat[] should work just fine as a setter method.
I recreated an example of this methodology in the test case below, in which I created a dataframe and used similar conditional statements and setting statements to test the output of using .iat[] vs .iloc. I was not able to replicate the issue.
dfA = pd.DataFrame([[0, 0, 3], [0, 4, 1], [10, 20, 30], [40, 60, 100]],
index=[0, 1, 2, 3], columns=['A', 'B', 'C'])
dfA
A B C
0 0 0 3
1 0 4 1
2 10 20 30
3 40 60 100
for i in range (1, len(dfA['A'])):
if dfA['B'].iat[i-1] > 0:
dfA.iat[i,2] = dfA['A'].iat[i-1]+dfA['B'].iat[i-1]
dfA
A B C
0 0 0 3
1 0 4 1
2 10 20 4
3 40 60 30
for i in range (1, len(dfA['A'])):
if dfA['B'].iat[i-1] > 0:
dfA.loc[i,'C'] = dfA.iat[i-1,0]+dfA.iat[i-1,1]
dfA
A B C
0 0 0 3
1 0 4 1
2 10 20 4
3 40 60 30