Skip to content

Avoid nans in rle #2180

@coxipi

Description

@coxipi

Generic Issue

Description

Currently,

da = 1,0,1,1,0
xclim.indices.run_length.rle(da) == 1,0,2,N,0

Nans are used to label values that are in a run, but not its first (or last) element.

This is used notably in keep_longest_run. We find the largest number (2), and ffill the surrounding nans, and it gives us the run of interest.

We could define a different rle

rle'(da) == 1,0,2,-1,0

And simply replace -1 with nans when needed. @SarahG-579462 has reported some benefits in sticking to int at a lower level. I wonder if more benefits could be obtained by sticking to int as much as possible.

This could also handled as:

out = 1,0,2,N,0
out.astype(np.int32) == 1,0,2,-2147483648,0

but at this point, we might as well use float32 with the nans

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions