Skip to content

BUG: consistency between logical ops & type casts #8151

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

Closed
wants to merge 1 commit into from

Conversation

behzadnouri
Copy link
Contributor

Closes #6528.
Currently series logical operations are bound to return a series with dtype='bool'. That being a good decision or not, it is good to have some internal consistency:

>>> a = pd.Series([nan, nan, nan])
>>> b = pd.Series([False, True, nan])
>>> a | b == b | a  # not symmetric
0     True
1    False
2     True
dtype: bool
>>> a & b == a.astype(bool) & b.astype(bool)  # not consistent w/ type casts
0     True
1    False
2    False
dtype: bool

With this patch, all these tests pass.

@@ -3418,7 +3444,7 @@ def test_comparison_label_based(self):

# identity
# we would like s[s|e] == s to hold for any e, whether empty or not
for e in [Series([]),Series([1],['z']),Series(['z']),Series(np.nan,b.index),Series(np.nan,a.index)]:
for e in [Series([]),Series([1],['z']),Series(['z']),Series(False,b.index),Series(False,a.index)]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a valid test
don't change it
you can add to tests but not change w/o a really good reason

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback in python

>>> bool(np.nan)
True

in pandas:

>>> pd.Series([np.nan], dtype='bool')
0    True
dtype: bool

this test passes only if np.nan is casted to False.

same comment for the other test.

@jreback
Copy link
Contributor

jreback commented Aug 31, 2014

need a release note

@jreback
Copy link
Contributor

jreback commented Mar 2, 2015

@behzadnouri can you rebase this. I believe there was another pr with modified this code.

@behzadnouri
Copy link
Contributor Author

this pr is based on the assumption that the element-wise logical operations are bound to return a series with dtype=bool. #9338 has changed this; so this way may not be the best way to go forward.

i feel like following python's mechanism of boolean operations is a better path.

@jreback
Copy link
Contributor

jreback commented Mar 3, 2015

can u provide an example of what you think is wrong / what needs changing?

@behzadnouri
Copy link
Contributor Author

I mean element-wise and/or etc should actually do element-wise logical operations, so then a & b and a | b should be equivalent of

>>> Series(map(lambda x, y: x and y, a, b))  # element-wise and
>>> Series(map(lambda x, y: x or y, a, b))  # element-wise or

for example:

>>> a = pd.Series(['a', '', np.nan])
>>> b = pd.Series([0, 1, 2])
>>> pd.Series(map(lambda x, y: x and y, a, b))
0    0
1
2    2
dtype: object
>>> pd.Series(map(lambda x, y: x or y, a, b))
0      a
1      1
2    NaN
dtype: object

#9338 is sort of doing this for integers, but same can be done for all types as in python "abc" and 3.14 is a valid expression.

@jreback jreback added this to the Next Major Release milestone May 9, 2015
@jreback
Copy link
Contributor

jreback commented May 9, 2015

@behzadnouri we ought to reexamine this for 0.17.0. pls rebase when you have a chance.

@behzadnouri
Copy link
Contributor Author

@jreback I close this one per my comment earlier that i no longer feel this is a good approach. I will do a new pr when i get a chance and if i could make it work.

@jreback
Copy link
Contributor

jreback commented May 10, 2015

great thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NaN values impact binary or operations asymmetrically
2 participants