Skip to content

BUG: Fix boolean comparison with a DataFrame on the lhs, and a list/tuple on the rhs GH4576 #4585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2013

Conversation

jreback
Copy link
Contributor

@jreback jreback commented Aug 16, 2013

closes #4576

In [1]: df = DataFrame(np.arange(6).reshape((3,2)))

In [2]:  b = np.array([2, 2])

In [3]:  b_r = np.atleast_2d([2,2])

In [4]:  b_c = b_r.T

In [5]: df>b
Out[5]: 
       0      1
0  False  False
1  False   True
2   True   True

In [6]: df.values>b
Out[6]: 
array([[False, False],
       [False,  True],
       [ True,  True]], dtype=bool)

In [7]: df>b_r
Out[7]: 
       0      1
0  False  False
1  False   True
2   True   True

In [8]: df.values>b_r
Out[8]: 
array([[False, False],
       [False,  True],
       [ True,  True]], dtype=bool)

In [9]: df>b_c
ValueError: cannot broadcast shape [(3, 2)] with block values [(2, 1)]

In [10]: df.values>b_c
ValueError: operands could not be broadcast together with shapes (3,2) (2,1) 

In [11]: df == b
Out[11]: 
       0      1
0  False  False
1   True  False
2  False  False

In [12]: df.values == b
Out[12]: 
array([[False, False],
       [ True, False],
       [False, False]], dtype=bool)

In [13]: df==b_r
Out[13]: 
       0      1
0  False  False
1   True  False
2  False  False

In [14]: df.values==b_r
Out[14]: 
array([[False, False],
       [ True, False],
       [False, False]], dtype=bool)

Numpy does weird things like this (bottom example), but we will raise
I believe it is actually a NotImplemented Type which is interpreted as False (but still very weird)

In [15]: df==b_c
ValueError: cannot broadcast shape [(3, 2)] with block values [(2, 1)]

In [16]: df.values==b_c
Out[16]: False

with list/tuple

In [2]: df>(2,2)
ValueError: operands could not be broadcast together with shapes (2,3) (2) 

In [3]: df>(2,2,2)
Out[3]: 
       0      1
0  False  False
1  False   True
2   True   True

In [4]: df>[2,2,2]
Out[4]: 
       0      1
0  False  False
1  False   True
2   True   True

In [5]: df==[2,2,2]
Out[5]: 
       0      1
0  False  False
1   True  False
2  False  False

This will work with a frame with non integer labels too

In [6]: df = DataFrame(np.arange(6).reshape((3,2)),columns=list('AB'),index=list('abc'))

In [7]: df==[2,2,2]
Out[7]: 
       A      B
a  False  False
b   True  False
c  False  False

jreback added a commit that referenced this pull request Aug 26, 2013
BUG: Fix boolean comparison with a DataFrame on the lhs, and a list/tuple on the rhs GH4576
@jreback jreback merged commit e25976a into pandas-dev:master Aug 26, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pandas 0.12 unexpected results when comparing dataframe to list or tuple
1 participant