@@ -362,11 +362,7 @@ def _convert_array_dtype(self, dtype: str, nodata: Union[float, None] = None) ->
362
362
nodata_change = nodata is not None and not utils .nan_equals (nodata , self .nodata )
363
363
array = self ._array
364
364
if nodata_change or unsafe_cast :
365
- # If necessary and possible, promote source dtype to be able to represent destination dtype exactly.
366
- # (This works around the (undocumented?) situation where even if a clipped array contains only values
367
- # that can be represented as the destination dtype, there is still casting overflow when these values
368
- # are near the dtype limits e.g. float32->int32 should be promoted to float64->int32. There is no way
369
- # of preventing this situation for float*->int64.)
365
+ # promote dtype to be able to represent destination dtype exactly (if possible) to clip correctly
370
366
array = array .astype (np .promote_types (self .dtype , dtype ), copy = True )
371
367
372
368
# round if converting from float to integer dtype
@@ -380,8 +376,8 @@ def _convert_array_dtype(self, dtype: str, nodata: Union[float, None] = None) ->
380
376
if src_info .min < dst_info .min or src_info .max > dst_info .max :
381
377
np .clip (array , dst_info .min , dst_info .max , out = array )
382
378
383
- # convert dtype
384
- with np .errstate (invalid = 'ignore' ): # ignore numpy warning for cast of nodata=nan to dtype
379
+ # convert dtype (ignoring numpy warnings for float overflow or cast of nan to integer)
380
+ with np .errstate (invalid = 'ignore' , over = 'ignore' ):
385
381
array = array .astype (dtype , copy = False , casting = 'unsafe' )
386
382
387
383
# set nodata value if it has changed, or may be invalid after rounding, clipping and casting
0 commit comments