dask / dask/dask

Selecting an empty slice with negative step size from a dask array returns a non-empty array.

Open
#10,555 0 comments 0 reactions 0 assignees View on GitHub
needs attention needs triage
Dominant language
Python
Stars
13.9k
Forks
2k
PR merge metrics
No merged PRs in 30d

Description

**Describe the issue**:

I found a case where the behavior of dask array slicing deviates from that of Numpy and others. The cause is an erroneous normalization in `dask.array.slicing.normalize_slice`. I have included a possible fix.

**Minimal Complete Verifiable Example**:

```python
import dask.array as da

# Dask behavior:
da.arange(3)[-5:-5:-2].compute()
=> array([2, 0])

# Regular Python behavior:
[0, 1, 2][-5,-5,-2]
=> []
```

This handling of slices in Dask is notably different from all other slicing functions in Python and Numpy. I tracked the problem down to the function `dask.array.slicing.normalize_slice`. In case the step size is negative, the start or end of the resulting slice is possibly set to `None` without checking whether the slice is empty or not.

**Anything else we need to know?**:

I think a possible fix would be to simplify `normalize_slice` to something like this:

```python
def normalize_slice(idx, dim):
if isinstance(idx, slice):
if math.isnan(dim):
return idx
return slice(*idx.indices)
return idx
```

**Environment**:

- Dask version: 2023.5.0
- Python version: 3.9.5
- Operating System: Linux
- Install method: pip

Contributor guide

Open the contributing guide

Research direction

Start with the minimal reproducer and inspect dask.array.slicing.normalize_slice, comparing its negative-step handling with Python and NumPy slice behavior. Add regression coverage for the empty slice case and verify that the computed Dask result is empty while existing slicing behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.