Ben Southgate
Ben Southgate

Reputation: 3808

Why does pandas use (&, |) instead of the normal, pythonic (and, or)?

I understand the pandas docs explain that this is the convention, but I was wondering why?

For example:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(6,4), index=list('abcdef'), columns=list('ABCD'))
print(df[(df.A < .5) | (df.B > .5)])
print(df[(df.A < .5) or (df.B > .5)])   

Returns the following:

          A         B         C         D
a -0.284669 -0.277413  2.524311 -1.386008
b -0.190307  0.325620 -0.367727  0.347600
c -0.763290 -0.108921 -0.467167  1.387327
d -0.241815 -0.869941 -0.756848 -0.335477
e -1.583691 -0.236361 -1.007421  0.298171
f -3.173293  0.521770 -0.326190  1.604712
Traceback (most recent call last):
  File "C:\test.py", line 64, in <module>
    print(df[(df.A < .5) or (df.B > .5)])   
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Upvotes: 8

Views: 1394

Answers (1)

salezica
salezica

Reputation: 76949

Because & and | are overridable (customizable). You can write the code that drives the operators for any class.

The logic operators and and or, on the other hand, have standard behavior that cannot be modified.

See here for the relevant documentation.

Upvotes: 17

Related Questions