Skip to content

Masking and preserving int type #3955

@zxdawn

Description

@zxdawn

When DataArray is masked by .where(), the type is converted to float64.

But, if we need to use the DataArray ouput from .where() in .isel(), the dtype should be int.
(#3949 )

MCVE Code Sample

import numpy as np
import xarray as xr

val_arr = xr.DataArray(np.arange(27).reshape(3, 3, 3),
                       dims=['z', 'y', 'x'])

z_indices = xr.DataArray(np.array([[1, 0, 2],
                                  [0, 0, 1],
                                  [-2222, 0, 1]]),
                         dims=['y', 'x'])

fill_value = -2222
sub = z_indices.where(z_indices != fill_value)
indexed_array = val_arr.isel(z=sub)

Expected Output

array([[ 1,  0,  2],
       [ 0,  0,  1],
       [nan,  0,  1]])

Problem Description

  File "E:\miniconda3\envs\satpy\lib\site-packages\xarray\core\indexing.py", line 446, in __init__
    f"invalid indexer array, does not have integer dtype: {k!r}"
TypeError: invalid indexer array, does not have integer dtype: array([[ 1.,  0.,  2.],
       [ 0.,  0.,  1.],
       [nan,  0.,  1.]])

Currently, pandas supports NaN values. Is this possible for xarray? or another method around?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions