Earlier I create a dataframe for every balance reading that contains a 0 on a particular day. But when it comes time to run the while loop, the if statement does not ever find a nonzero balance reading, even when it does exist.
I independently ran the code for one particular account and it passed through all my code and found the previous balance and replaced it, but I had to do it sequentially, not all in one function. My guess is that itertuples is the source of this problem.
# df looks like this with amount_y being the balance (ignore amount_x for now, it represents transaction amount/day/account):
# accrual_date | financial_account_id | amount_x | amount_y
def updateBalance(df):
index = 0
for row in df.itertuples():
old_balance = 0
index_when_condition_met = 0
if df.loc[index,'amount_y'] == 0:
financial_account_id_search = df.loc[index,'financial_account_id']
accrual_date_search = df.loc[index,'accrual_date']
ordered = df[df['financial_account_id'].str.match(financial_account_id_search)].sort_values(by='accrual_date').reset_index(drop=True)
for row2 in ordered.itertuples():
index_when_condition_met = ordered[(ordered['accrual_date'].str.match(accrual_date_search)) & (ordered['amount_y'] == 0)].index.values[0] #takes first balance reading if more than one balance reading of 0 took place on same day
while index_when_condition_met > 0:
if ordered.loc[index_when_condition_met - 1,'amount_y'] != 0: # previous balance is not 0,
old_balance = ordered.loc[index_when_condition_met - 1,'amount_y']
break
else: # previous balance is 0, need to check earlier entries
index_when_condition_met = index_when_condition_met - 1
if index_when_condition_met == 0:
old_balance = 0
df.at[index,"amount_y"] = old_balance # might be redundant
else:
df.at[index,"amount_y"] = old_balance
index = index + 1
return df
updateBalance(df)
The new resulting dataframe for e should have replaced all 0 values with a nonzero previous balance (if it exists).