Fix idxmax/idxmin silently promoting integer label coordinate dtype to float64 (GH#7527) - #11544
Fix idxmax/idxmin silently promoting integer label coordinate dtype to float64 (GH#7527)#11544CAOShurong wants to merge 2 commits into
Conversation
…o float64 (GH#7527) Previously, on floating-point data the integer coordinate labels of idxmax/idxmin results were silently cast to float64 even for fully-valid reduction slices, because _calc_idxminmax called res.where unconditionally. Now .where only runs for non-chunked inputs when a slice is actually all-NaN, so valid slices keep their coordinate dtype. Chunked (dask) inputs keep the original delayed .where to avoid forcing an extra compute(). Closes pydata#7527
77369bf to
5d9750e
Compare
|
Pushed a fixup after the first CI run flagged Root cause of that failure: my initial guard Fix (amended): for chunked (dask) inputs we keep the original unconditional lazy |
for more information, see https://pre-commit.ci
Description
DataArray.idxmax/idxmin(and theDatasetequivalents) silently promoted the dtype of the returned label coordinate tofloat64whenever the input data was floating-point — even when the reduced dimension's coordinate was an integer (e.g.int64). This is GH#7527, open since 2023 with no fix.Root cause: in
xarray/computation/computation.py::_calc_idxminmax, theres.where(~allna, fill_value)step ran unconditionally for every floating-point input. Calling.wherewith the default floatfill_valuecast the integer label coordinate to float, even for fully-valid reduction slices that never needed filling.Fix: only call
.wherewhen at least one slice is actually all-NaN(allna.any()). Fully-valid slices keep their original coordinate dtype; all-NaNslices still get the floatfill_valueas before.Checklist
whats-new.rstapi.rst(none added)AI Disclosure
Tools: OpenAI Codex CLI / GPT-5-Codex (assisted reproduction, root-cause trace, and regression test); all changes verified by running the xarray test suite locally (new regression test + existing
TestReduce1Didxmin/idxmax suites + docstring doctests all pass).