larray-project / larray

N-dimensional labelled arrays in Python
https://larray.readthedocs.io/
GNU General Public License v3.0
8 stars 6 forks source link

where() using X.axis expressions gives wrong result silently #1083

Closed gdementen closed 1 year ago

gdementen commented 1 year ago
>>> arr = ndtest("age=0..5")
>>> where(X.age == 3, 42, arr)
age   0   1   2   3   4   5
     42  42  42  42  42  42
>>> where(X.age == 3, arr, 42)
age  0  1  2  3  4  5
     0  1  2  3  4  5

I suspect this is because larray.core.expr.BinaryOp does not override __bool__ and is thus truthy.

As a related note, the multiply-by-filter workaround, only works when the array is first. I suspect the r* variants of ExprNode binops are broken. This is correct :

>>> arr * (X.age != 3) + (X.age == 3) * 42
age  0  1  2   3  4  5
     0  1  2  42  4  5
>>> arr * (X.age != 3) + 42 * (X.age == 3)
age  0  1  2   3  4  5
     0  1  2  42  4  5

This is not correct (but making this work is a only high priority, not blocker priority, as this isn't silent) :

>>> (X.age != 3) * arr + 42 * (X.age == 3)
<larray.core.expr.BinaryOp at 0x29f8e542cd0>
>>> 42 * (X.age == 3) + arr * (X.age != 3)
<larray.core.expr.BinaryOp at 0x29f9f4bf610>
>>> (X.age == 3) * 42 + arr * (X.age != 3)
<larray.core.expr.BinaryOp at 0x29f9f4bd150>
gdementen commented 1 year ago

closed in 3796f9baf80a783d14eae34351212173ed1d1cc5, 1dfa7a2ce8feea32284769d5269b4cedfe874c07 and 22ac89a6eb23e1dcc3132d23aa36d2f26acfa340