Search code examples
pythonpandasdataframeyahoo-finance

Pandas: "if" block does not get executed


When I execute below code, it only executes "else" clause even when "if" condition is satisfied. For example

 Date   05/01/2006
 Open   5521.86
 Low    5502.25 
 Close.shift(1) 5523.620117

It satisfies "if" block condition (i.e.)

 DAX_Logic < 1 *(value is 0.99968132240039786)*

However the "if" block never gets executed. Please assist

DAX=pd.io.data.DataReader("^GDAXI","yahoo",start,end)
DAX_Logic= DAX['Open'].fillna(1) / DAX['Close'].shift(1).fillna(1)
DAX_Logic_1=  DAX['Low'].fillna(1) / DAX['Open'].fillna(1) 
if DAX_Logic.any() < 1:
    Daily_Return = 100*(DAX['Low']-DAX['Open'])
else:
    Daily_Return = (-100)*(DAX['Close']-DAX['Open'])  

I tried using the following piece of code and the below piece only executes "If" block and never "else" block... Bit confused what I am doing wrong in above and below code.(They produce completely opposite results)

DAX=pd.io.data.DataReader("^GDAXI","yahoo",start,end)
DAX_Logic= DAX['Open'].fillna(1) / DAX['Close'].shift(1).fillna(1)
DAX_Logic_1=  DAX['Low'].fillna(1) / DAX['Open'].fillna(1) 
for i in DAX_Logic.index[:]:
    if (DAX_Logic[i] < 1):
        Daily_Return = 100*(DAX['Low']-DAX['Open'])
    else:
        Daily_Return = (-100)*(DAX['Close']-DAX['Open'])  

Solution

  • Currently, you are running general purpose python with for and if in place of vectorized pandas operations such as conditionally creating columns (i.e., series) by logical indexing with another series.

    Consider first adding DAX_Logic and DAX_Logic_1 as new columns in DAX dataframe inline with other rows, allowing you to use loc to create Daily_Return column:

    DAX = pd.io.data.DataReader("^GDAXI","yahoo",start,end)
    # NEW COLUMNS (NOT SEPARATE SERIES)
    DAX['DAX_Logic'] = DAX['Open'].fillna(1) / DAX['Close'].shift(1).fillna(1)
    DAX['DAX_Logic_1'] = DAX['Low'].fillna(1) / DAX['Open'].fillna(1)
    
    # DAILY RETURN
    DAX.loc[DAX['DAX_Logic'] > 1, 'Daily_Return'] = 100*(DAX['Low']-DAX['Open'])
    DAX.loc[DAX['DAX_Logic'] <= 1, 'Daily_Return'] = (-100)*(DAX['Close']-DAX['Open'])
    

    Alternatively, use numpy's np.where() for vectorized if/else calculations:

    import numpy as np
    
    DAX['Daily_Return'] = np.where(DAX['DAX_Logic'] > 1, 100*(DAX['Low']-DAX['Open']), (-100)*(DAX['Close']-DAX['Open']))
    

    And if you want Daily_Return to standalone outside of a dataframe column, simply assign it since every column in a pandas dataframe is a pandas series:

    Daily_Return = DAX['Daily_Return']