0

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)
delalma
  • 838
  • 3
  • 12
  • 24

2 Answers2

0

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

What do you mean by "the code does not work". Is it not executing? If yes - the reason for that is because you have a while False loop, which means it will never enter that loop. Also, when you use

while True

you basically make an infinite loop. You need a break statement to stop it, or try putting it in a function so whenever a block of code is executed, function will return. If you still don't understand what "while True" loop means, check this link out:

What does "while True" mean in Python?

Also check this out to learn about break and continue statements if you don't know anything about them:

https://docs.python.org/3/tutorial/controlflow.html

So, put everything you just said in if statements in proper fashion. Then the loop will end whenever it hits certain if statement. For example, make it break if it won't find certain type of data that it was meant to search. Or break out of the loop if it finds it.

todovvox
  • 170
  • 10