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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -650,4 +650,4 @@ Bug Fixes
- Bug in accessing groups from a ``GroupBy`` when the original grouper
was a tuple (:issue:`8121`).


- Bug in logical operations of series and their consistency with type casts (:issue:`6528`)
8 changes: 5 additions & 3 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,10 +636,12 @@ def wrapper(self, other):
if isinstance(other, pd.Series):
name = _maybe_match_name(self, other)

other = other.reindex_like(self).fillna(False).astype(bool)
return self._constructor(na_op(self.values, other.values),
left = self.astype(bool).values
right = other.astype(bool).reindex_like(self)
right = right.fillna(False).values
return self._constructor(na_op(left, right),
index=self.index,
name=name).fillna(False).astype(bool)
name=name)
elif isinstance(other, pd.DataFrame):
return NotImplemented
else:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4992,7 +4992,7 @@ def test_logical_with_nas(self):
# GH4947
# bool comparisons should return bool
result = d['a'] | d['b']
expected = Series([False, True])
expected = Series(np.logical_or(d['a'], d['b']), dtype='bool')
Copy link
Contributor

Choose a reason for hiding this comment

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

don't change this test
you can add to but not change tests

assert_series_equal(result, expected)

# GH4604, automatic casting here
Expand Down
28 changes: 26 additions & 2 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2368,6 +2368,31 @@ def check(series, other, check_reverse=False):
check(self.ts, 5, check_reverse=True)
check(tm.makeFloatSeries(), tm.makeFloatSeries(), check_reverse=True)

def test_bool_op(self): # GH6528 & GH8151
u = pd.Series([nan, nan, nan, False, False, True, True])
v = pd.Series([nan, False, True, False, True, False, True])

for a, b in [[u, v], [v, u]]:
# against numpy logical casted to bool
assert_series_equal(a | b, Series(np.logical_or(a, b), dtype='bool'))
assert_series_equal(a & b, Series(np.logical_and(a, b), dtype='bool'))
assert_series_equal(a ^ b, # np.logical_xor throws with floats!
Series(np.logical_xor(a.values.astype(bool),
b.values.astype(bool))))

assert_series_equal(~ a.astype(bool),
Series(np.logical_not(a), dtype='bool'))

# type cast semantics
assert_series_equal(a | b, a.astype(bool) | b.astype(bool))
assert_series_equal(a & b, a.astype(bool) & b.astype(bool))
assert_series_equal(a ^ b, a.astype(bool) ^ b.astype(bool))

# symmetry
assert_series_equal(a & b, b & a)
assert_series_equal(a | b, b | a)
assert_series_equal(a ^ b, b ^ a)

def test_neg(self):
assert_series_equal(-self.series, -1 * self.series)

Expand Down Expand Up @@ -3418,7 +3443,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.

result = a[a | e]
assert_series_equal(result,a[a])

Expand Down Expand Up @@ -6144,4 +6169,3 @@ def test_unique_data_ownership(self):
if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
exit=False)