Skip to content

dtype and shape don't match for empty arrays #1398

@jcrist

Description

@jcrist

Currently if an array is empty, we just return np.empty(0), which has a dtype f8. This means that dtype of the output won't match for other dtypes. Further, you can have "empty" arrays with shape != () as long as one of the dimensions is 0 (e.g. shape=(0, 10, 10)).

The da.Array objects have the necessary metadata to create empty arrays with the appropriate dtype/shape, but these aren't passed on to _finalize. One solution for this would be to make _finalize a property, and return a partial with the metadata if the output is known to be empty. This slightly complicates the implementation, but isn't too bad. I'll do it here:

def finalize_empty(shape, dtype, res):
    return np.empty(shape, dtype=dtype)

class Array(Base):
    ...
    @property
    def _finalize(self):
        if self.shape or 0 not in self._shape:
            return finalize
        return partial(finalize_empty, self.shape, self.dtype)

This would solve the issue and is (I think) pretty simple, but I'll open it here for discussion. This also might be a non-issue, as we might not care if the metadata matches for empty arrays.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions