min and max methods changing dtype to float64
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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