Skip to content

Arithmetic with numpy arrays: inconsistency depending on mask #6734

@rcomer

Description

@rcomer

What happened:
If I multiply a plain numpy array by a dask array, I get a dask array. But if I multiply a numpy masked array by a dask array, I get a numpy masked array.

What you expected to happen:
I would expect to either get a dask array in both cases, or the type of my first operand in both cases.

Minimal Complete Verifiable Example:

import numpy as np
import dask.array as da

plain_numpy_array = np.arange(5)
masked_numpy_array = np.ma.array(np.arange(5), mask=[0, 1, 1, 0, 0])
dask_array = da.from_array(plain_numpy_array)

print(type(plain_numpy_array * dask_array))
print(type(masked_numpy_array * dask_array))

output:

<class 'dask.array.core.Array'>
<class 'numpy.ma.core.MaskedArray'>

Anything else we need to know?:
This relates to #4441, and I note the advice at #4441 (comment) to explicitly convert the numpy arrays to dask arrays before the arithmetic operation. I am attempting to do this for Iris (SciTools/iris#3790) but, while teasing out the details of that, we noticed this inconsistency between plain numpy arrays and masked numpy arrays. I don't think the inconsistency was noted at #4441, and it possibly throws a new light on it?

Environment:

  • Dask version: 2.26.0
  • Python version: 3.7.8
  • Operating System: RHEL7.8
  • Install method (conda, pip, source): miniconda

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