I am looping through some data, and if the column doesn't contain any NaNs I want to merge this with my master df
. But for some reason, .isna().any()
doesn't work in the loop, only when I look at the column separately.
My code currently is:
if df[Stock].isna().any() is False:
total_df = total_df.merge(df[['Date', Stock]], on='Date', how='left')
else:
pass
As far as I'm concerned this should filter out any columns containing NaNs. However, it doesn't, it basically appears to do nothing as my df
once it's finished contains columns of NaNs as well as the columns I actually want. I have also tried ==True
, but to no avail. When I check a column that I know for a fact has NaNs in it using print(df[Stock].isna().any())
the program quite rightly returns True
. So my question is why won't this work in a loop?
I've been starring at this for hours now and it's possible I'm doing something incredibly stupid so a fresh pair of eyes might be whats needed but I'm really stumped. Cheers
EDIT:
So for what it's worth, when I do the sum of each column with NaNs in, it returns an actual number. So it appears pandas isn't recognising what are quite clearly nan
s when I check the df
manually. However, I have to do pd.to_numeric
on my df
data before performing the loop other wise I can't do any maths on the price data at a later stage. Is it possible this is affecting things?