pydata / pydata/xarray

min and max methods changing dtype to float64

Open
#6,876 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-faq
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

I'm working with datasets that have coordinates with lots of decimal places. As I don't need this I reassign cords and transform the dtype to float32.
For calculating the extent of the dataset a standard procedure is to use the min() and max() methods but using them over the float32 coordinates yields the float64 values which leads to misaligned coordinates.

Minimum reproducible example:

import xarray as xr
import numpy as np
 
da = xr.DataArray(
    data=np.array([1.2, 2.2, 3.3], dtype=np.float32),
    coords={"x": [1.345, 2.345, 3.345]},
    dims=("x",),
)
 
print(da[0].dtype)  # dtype('float32')
print(da.min().dtype)  # dtype('float64')
print(type(np.min(da.values)))  # dtype('float32')

Is there a reason for this?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior with the Python example, comparing xarray's DataArray.min() and DataArray.max() with NumPy's reductions on float32 data. Trace the reduction path in xarray and verify that the completed change preserves the input dtype for these methods without breaking the reported minimum and maximum values.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.