-1

My concrete example:

In: list(pd.unique(df['x'].ravel())) 
Out: [nan, 1.0]

But when I check if this is true:

In: list(pd.unique(df['x'].ravel())) is [nan, 1.0]
Out: False

Anybody can help me understand what am I doing wrong?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Gunnar
  • 105
  • 5
  • If you disagree with the interpreter, it is almost certainly right. – msw Feb 19 '16 at 20:12
  • Your question presupposes "you know it's true" therefore your assumption is wrong. You put your understanding up against the interpreter's behaviour and your understanding was found wanting. My point is simply that it is a better starting assumption that the mistake is yours. – msw Feb 19 '16 at 22:39
  • I may have explained myself wrong. Of course I am trying to test things I don't know. This is just a little check before I use this test a thousand times. I am trying to discriminate the unique responses of a column in pandas. – Gunnar Feb 20 '16 at 19:16

3 Answers3

3

is tests to see if the expressions on each side are the same object. To see if they are equivalent, use ==.

zondo
  • 19,901
  • 8
  • 44
  • 83
  • Thank you. What I am actually trying to test is if the output `[nan, 1.0]` is inside a set, which if by construction I know it is true, it still returns as false. That is why I needed to check if it was the same object rather than checking if it was equivalent. Should I re-formulate the question? – Gunnar Feb 19 '16 at 18:37
  • I don't quite understand what you mean by testing if the output is inside a set, but if you use `is` there, it is impossible for it ever to evaluate to `True`, because you are creating a new list for the comparison. – zondo Feb 19 '16 at 18:40
  • My set would be `s=[[nan,1.0],[0,1]]` and I would like to test if the output of `list(pd.unique(df['x'].ravel()))`, which I know it's `[nan, 1.0]`, is inside `s`, which I also know it's true. So, would the correct syntax be `list(pd.unique(df['x'].ravel())) in s`? The output I get from this is `False` but I am quite sure I am doing everything wrong here after readin the posts. TIA – Gunnar Feb 19 '16 at 18:47
0

Either way, even if you were to properly use ==, you'd still get False. A quick workaround would be to filter out nan's:

[x for x in list(pd.unique(df['x'].ravel())) if str(x) != 'nan'] == ...

See why "NaN" doesn't equal "NaN".

Community
  • 1
  • 1
leongold
  • 1,004
  • 7
  • 14
0

I think you need test like:

In [73]: df['x']
Out[73]: 
0    np.nan
1         1
Name: x, dtype: object

In [74]: list(pd.unique(df['x'].ravel())) == ['np.nan', '1']
Out[74]: True
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252