Skip to content

Commit 6e29afb

Browse files
committed
Upcast floats to at most float32, not float64
1 parent b3627f9 commit 6e29afb

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

doc/whats-new.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ Enhancements
5252
2D variable.
5353
By `Deepak Cherian <https://github.com/dcherian>`_.
5454
- Reduce memory usage when decoding a variable with a scale_factor, by
55-
converting 8-bit and 16-bit integers to float32 instead of float64 (:pull:`1840`).
55+
converting 8-bit and 16-bit integers to float32 instead of float64
56+
(:pull:`1840`), and keeping float16 and float32 as float32 (:issue:`1842`).
5657
By `Zac Hatfield-Dodds <https://github.com/Zac-HD>`_.
5758

5859
.. _Zarr: http://zarr.readthedocs.io/

xarray/coding/variables.py

+4
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ class CFScaleOffsetCoder(VariableCoder):
215215
@staticmethod
216216
def _choose_float_dtype(data, has_offset):
217217
"""Return a float dtype sufficient to losslessly represent `data`."""
218+
# Keep float32 as-is. Upcast half-precision to single-precision,
219+
# because float16 is "intended for storage but not computation"
220+
if data.dtype.itemsize <= 4 and np.issubdtype(data.dtype, np.floating):
221+
return np.float32
218222
# float32 can exactly represent all integers up to 24 bits
219223
if data.dtype.itemsize <= 2 and np.issubdtype(data.dtype, np.integer):
220224
# A scale factor is entirely safe (vanishing into the mantissa),

0 commit comments

Comments
 (0)