0

I have a data frame df and column col. The value of the last entry is NaN, like this

df['col'][-1:]

48   NaN
Name: col, dtype: float64

I'm trying to apply a condition on that last value, but running this gives a ValueError:

if dcc_td['da_4'][-1:].isna():
    print('Missing value.')

I did some search and it seems converting the series to boolean value may help. But running both of these conditions gives the same result:

if df['col'][-1:].isna().bool():
    print('Missing value.')

Missing value.

And:

if ~df['col'][-1:].isna().bool():
    print('Missing value.')

Missing value.

Further, if I run this I get True:

df['col'][-1:].isna().bool()

But if I run this I get -2, which I expected to see False:

~df['col'][-1:].isna().bool()

What did I misunderstand about these concepts?

EDIT: A simple solution for the ValueError issue is:

if df['col'][-1:].isna().all():
    print('Missing value.')

But still, the boolean issue is quite helpful to understand.

NonSleeper
  • 849
  • 1
  • 6
  • 17

1 Answers1

0

~ is bitwise operator, when you do ~True you are actually doing ~1 which will invert the each bit from binary representation of 1. [1]

What you want is logical operator not.

[1] Why does bitwise "not 1" equal -2?

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • Thanks. It takes some time for me to understand this. In the meantime I find this `~dcc_td['da_4'][-1:].isna().all()` also gives `False`. – NonSleeper Aug 18 '22 at 15:53
  • @NonSleeper `.all()` returns `numpy.bool_`, doing bit-wise NOT on numpy boolean can reference https://stackoverflow.com/questions/13728708/inverting-a-numpy-boolean-array-using – Ynjxsjmh Aug 18 '22 at 15:59