You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Usage: confused by np.where behaviour and fill_value #871
Please provide a description of what you'd like to do.
More dependable fill_value preservation in where
Example Code
importsparseimportnumpyasnparray=sparse.COO.from_numpy(np.eye(3), fill_value=0)
assertnp.where(array==1, np.nan, array).fill_value==0# passesassertnp.where(array<1, np.nan, array).fill_value==0# fails, result fill_value is np.nan
For mask=array==1, mask.fill_value=False and the array's fill_value is preserved in the np.where call.
For mask=array<1, mask.fill_value=True and the array's fill_value is NOT preserved in the np.where call.
This kind of behaviour is hard to rely on in a library. Is it a bug?
The reasoning is the following: fill_values propagate through elemwise because we expect elemwise fns to produce mostly fill_values. If it was otherwise, we'd need to densify.
If we still want to add this in the docstring, we can do so somewhere around here. We can also take a much simpler example to explain how fill_value is handled for element wise operations. Here's an example with + operation :
Activity
dcherian commentedon May 15, 2025
OK I understand what's happening now.
np.whereis basically applied tomask.fill_value, and the.dataare filled appropriately.It would be good to call this behaviour out in the docstring
hameerabbasi commentedon May 15, 2025
In general,
out_fv = elemwise_fn(*in_fvs). So ifarray.fill_value = 0, and for a scalar, it is its own fill value.The reasoning is the following: fill_values propagate through elemwise because we expect elemwise fns to produce mostly fill_values. If it was otherwise, we'd need to densify.
dcherian commentedon May 15, 2025
Got it. thanks! Feel free to close if you don't think the docstring needs to be updated.
prady0t commentedon May 27, 2025
If we still want to add this in the docstring, we can do so somewhere around here. We can also take a much simpler example to explain how
fill_valueis handled for element wise operations. Here's an example with+operation :Mentioning "Since
out_fv = elemwise_fn(*in_fvs)we only have to calculatefill_valueonce and hence sparse calculations saves memory".docs: explain fill-value propagation in where (#987)