10

pd.DataFrame.all and pd.DataFrame.any convert to bool all values and than assert all identities with the keyword True. This is ok as long as we are fine with the fact that non-empty lists and strings evaluate to True. However let assume that this is not the case.

>>> pd.DataFrame([True, 'a']).all().item()
True  # Wrong

A workaround is to assert equality with True, but a comparison to True does not sound pythonic.

>>> (pd.DataFrame([True, 'a']) == True).all().item()
False  # Right

Question: can we assert for identity with True without using == True

MLguy
  • 1,776
  • 3
  • 15
  • 28
  • A comparison to True is not unpythonic if you want to assert that a value is equal to True (and not just truthy). In fact, that's exactly what comparisons are for. – Aran-Fey Mar 26 '18 at 18:54

2 Answers2

17

First of all, I do not advise this. Please do not use mixed dtypes inside your dataframe columns - that defeats the purpose of dataframes and they are no more useful than lists and no more performant than loops.

Now, addressing your actual question, spolier alert, you can't get over the ==. But you can hide it using the eq function. You may use

df.eq(True).all()

Or,

df.where(df.eq(True), False).all()

Note that

df.where(df.eq(True), False)

       0
0   True
1  False

Which you may find useful if you want to convert non-"True" values to False for any other reason.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • Out of curiosity: Is there any reason to use `df.where` or did you just include it in your answer for the heck of it? – Aran-Fey Mar 26 '18 at 18:57
  • @Aran-Fey It is a useful intermediate step if you want to coerce non-"True" values to False for some other reason besides just calling `.all`. Additionally, I wanted to flex my fingers after a good night's sleep ;) – cs95 Mar 26 '18 at 18:58
  • 1
    I've basically stopped to read after the first paragraph and accepted the answer :) – MLguy Mar 26 '18 at 20:25
3

I would actually use

(pd.DataFrame([True, 'a']) == True).all().item()

This way, you're checking for the value of the object, not just checking the "truthy-ness" of it.

This seems perfectly pythonic to me because you're explicitly checking for the value of the object, not just whether or not it's a truthy value.

wpercy
  • 9,636
  • 4
  • 33
  • 45
  • `pd.DataFrame([True]) is True` returns `False` – MLguy Mar 26 '18 at 18:45
  • 1
    You're right. In that case, I would say that my argument still stands where checking `== True` is perfectly pythonic because you're explicitly checking for the value of the object, not the truthy-ness of it – wpercy Mar 26 '18 at 18:47
  • 1
    https://stackoverflow.com/questions/36825925/expressions-with-true-and-is-true-give-different-results – roganjosh Mar 26 '18 at 18:47