Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing xarray_reduce parameter dtype not doing anything #388

Closed
claytharrison opened this issue Sep 4, 2024 · 3 comments · Fixed by #389
Closed

Passing xarray_reduce parameter dtype not doing anything #388

claytharrison opened this issue Sep 4, 2024 · 3 comments · Fixed by #389

Comments

@claytharrison
Copy link

claytharrison commented Sep 4, 2024

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
@xarray-contrib xarray-contrib deleted a comment from GenaroSalomone Sep 4, 2024
@dcherian
Copy link
Collaborator

dcherian commented Sep 4, 2024

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.

@claytharrison
Copy link
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')

@dcherian
Copy link
Collaborator

dcherian commented Sep 4, 2024

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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants