pydata / pydata/sparse

Usage: confused by `np.where` behaviour and `fill_value`

Open
#871 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation
Dominant language
Python
Stars
668
Forks
141
Avg merge
2d 8h
Merged PRs (30d)
4

Description

Please provide a description of what you'd like to do.

More dependable fill_value preservation in where

Example Code
import sparse
import numpy as np

array = sparse.COO.from_numpy(np.eye(3), fill_value=0)
assert np.where(array == 1, np.nan, array).fill_value == 0  # passes
assert np.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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two np.where examples from the issue with sparse.COO.from_numpy and compare the mask fill values. Trace the np.where entry point and its fill-value handling, then establish consistent preservation behavior and regression coverage for both comparisons.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.