Skip to content

Passing xarray_reduce parameter dtype not doing anything #388

Closed

Description

@claytharrison

When I try passing a dtype to xarray_reduce, the output is still uint64 for some reason:

import xarray as xr
from flox.xarray import xarray_reduce

ds = xr.Dataset(
    {
        "counts": ("time", np.array([0,2,1,0,1])),
        "group": ("time", np.array([1,1,1,2,2]))
    },
)
xarray_reduce(ds["counts"],
                ds["group"],
                expected_groups=(np.array([1,2]),),
                func="sum",
                dtype="uint8")
<xarray.DataArray 'counts' (group: 2)> Size: 16B
array([3, 1], dtype=uint64)
Coordinates:
  * group    (group) int64 16B 1 2

This happens whether I use "uint8" as a string or np.uint8.

Of course, I can just use .astype after the fact, but I expect this is an allowed parameter for some reason or another?

Python 3.12.5
flox               0.9.10
xarray             2024.7.0

Activity

  1. dcherian commented on Sep 4, 2024

    @dcherian
    Collaborator

    Yes, I'm following numpy here:

    np.sum(ds.counts.data).dtype  # np.uint64
    

    https://numpy.org/doc/stable/reference/generated/numpy.sum.html

    The dtype of a is used by default unless a has an integer dtype of less precision than the default platform integer. In that case, if a is signed then the platform integer is used while if a is unsigned then an unsigned integer of the same precision as the platform integer is used.

  2. claytharrison commented on Sep 4, 2024

    @claytharrison
    Author

    Yes but if I pass a dtype argument explicitly, then it should do something, right?

    >>> np.sum(np.array([1, 2, 3], dtype=np.int8)).dtype
    dtype('int64')
    >>> np.sum(np.array([1, 2, 3], dtype=np.int8), dtype=np.int8).dtype
    dtype('int8')
  3. dcherian commented on Sep 4, 2024

    @dcherian
    Collaborator

    haha, yes, totally misinterpreted the docs there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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