Skip to content
Advertisement

While loop with missing data handling

I would like to loop over a file where there is an date index and values. Looping backward would help to prevent checking a date where there is no data. So it should stop as it find the first date. I set a today date, change into the appropriate format that correspond to the date in the file, try to loc the data and change the date to previous day if no data found. I think a while look is good for that. The logic seems ok but the code does not work. Any help would be appreciated. My code is:

fiat_balance = []
today = datetime.today()
while False:
  date = today.strftime('%m/%d/%Y')
  date = date+' 10:30:00'
  fiat_balance = pd.concat([cp.loc[date],pt.loc[date]], axis=0, keys=[2,3]).fillna(0)
  today-= timedelta(days=1)

Advertisement

Answer

What exactly do you think while False will achieve?

If you hope to enter that loop, backward or otherwise, that is the wrong thing to do.

Instead, you should probably have something like:

fiat_balance = []
today = datetime.today()
while True:
    date = today.strftime('%m/%d/%Y 10:30:00')
    fiat_balance = pd.concat([cp.loc[date],pt.loc[date]], axis=0, keys=[2,3]).fillna(0)
    if thatWorked:
        break
    today -= timedelta(days=1)

Of course, you’ll have to define what thatWorked is for your particular scenario, I’ve just put it in as a place-holder.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement