Skip to content
Advertisement

python jupyter Same condition tested in an if statement behave differently

Have a Jupyter Lab notebook which at a certain point compares two dataframes. df_lastweek is an extraction of only last week’s data while the df_lastmonth is the extraction of the last 30 days. The two dataframes are different the latter having more rows than the former.

The following if comparing the two different dataframes does not trigger:

if not df_lastweek.equals(df_lastmonth):
    df_lastmonth.describe()

while the next cell uses the SAME statement and does trigger as expected: if not df_lastweek.equals(df_lastmonth): regplot_of_df(df_lastmonth, 2000) and regularly call the regplot_of_df function plotting the data as expected.

Tried inverting the two cells but the describe statement never gets called.

No clue as to what I’m missing. Follows some more data about the two dataframes and also if anyone cares to check all of the notebook you may find it here: , where the “offending” cells are the very last three: [https://github.com/rjalexa/blood-pressure/blob/master/bplogs_analyze.ipynb][1]

df_lastweek.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 10 entries, 0 to 9
Data columns (total 12 columns):
 #   Column            Non-Null Count  Dtype         
---  ------            --------------  -----         
 0   Date              10 non-null     object        
 1   partofday         10 non-null     object        
 2   Time              10 non-null     object        
 3   SYS               10 non-null     int64         
 4   DIA               10 non-null     int64         
 5   Pulse             10 non-null     int64         
 6   Measurement Date  10 non-null     object        
 7   datetime          10 non-null     datetime64[ns]
 8   unix              10 non-null     float64       
 9   elapsed_seconds   10 non-null     float64       
 10  bp_stage          10 non-null     object        
 11  bp_color          10 non-null     object        
dtypes: datetime64[ns](1), float64(2), int64(3), object(6)
memory usage: 1.0+ KB

df_lastmonth.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 21 entries, 0 to 20
Data columns (total 12 columns):
 #   Column            Non-Null Count  Dtype         
---  ------            --------------  -----         
 0   Date              21 non-null     object        
 1   partofday         21 non-null     object        
 2   Time              21 non-null     object        
 3   SYS               21 non-null     int64         
 4   DIA               21 non-null     int64         
 5   Pulse             21 non-null     int64         
 6   Measurement Date  21 non-null     object        
 7   datetime          21 non-null     datetime64[ns]
 8   unix              21 non-null     float64       
 9   elapsed_seconds   21 non-null     float64       
 10  bp_stage          21 non-null     object        
 11  bp_color          21 non-null     object        
dtypes: datetime64[ns](1), float64(2), int64(3), object(6)
memory usage: 2.1+ KB



  [1]: https://github.com/rjalexa/blood-pressure/blob/master/bplogs_analyze.ipynb

Advertisement

Answer

The if works, it just doesn’t show the result as it’s not the last expression in the cell. You need do use display:

from IPython.display import display

if not df_lastweek.equals(df_lastmonth):
    display(df_lastmonth.describe())

Alternatively, you may set InteractiveShell.ast_node_interactivity to 'all'.

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