Skip to content

Commit

Permalink
return a simple bool for uncomparable data
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Jan 11, 2024
1 parent 367d63c commit 5bc36ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
10 changes: 7 additions & 3 deletions astropy/table/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -3699,7 +3699,11 @@ def _rows_equal(self, other):
This is actual implementation for __eq__.
Returns a 1-D boolean numpy array showing result of row-wise comparison.
Returns a 1-D boolean numpy array showing result of row-wise comparison,
or a bool (False) in cases where comparison isn't possible (uncomparable dtypes
or unbroadcastable shapes). Intended to follow legacy numpy's elementwise
comparison rules.
This is the same as the ``==`` comparison for tables.
Parameters
Expand Down Expand Up @@ -3740,12 +3744,12 @@ def _rows_equal(self, other):
# numpy may complain that structured array are not comparable (TypeError)
# or that operands are not brodcastable (ValueError)
# see https://github.com/astropy/astropy/issues/13421
result = np.array([False])
result = False
else:
try:
result = self.as_array() == other
except whitelist:
result = np.array([False])
result = False

return result

Expand Down
20 changes: 16 additions & 4 deletions astropy/table/tests/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2468,7 +2468,19 @@ def test_mixin_join_regression():
)
def test_table_comp(t1, t2):
# see https://github.com/astropy/astropy/issues/13421
assert not any(t1 == t2)
assert not any(t2 == t1)
assert all(t1 != t2)
assert all(t2 != t1)
try:
np.result_type(t1.dtype, t2.dtype)
np.broadcast_shapes((len(t1),), (len(t2),))
except (TypeError, ValueError):
# dtypes are not comparable or arrays can't be broadcasted:
# a simple bool should be returned
assert not t1 == t2
assert not t2 == t1
assert t1 != t2
assert t2 != t1
else:
# otherwise, the general case is to return a 1D array with dtype=bool
assert not any(t1 == t2)
assert not any(t2 == t1)
assert all(t1 != t2)
assert all(t2 != t1)

0 comments on commit 5bc36ec

Please sign in to comment.