@@ -1401,18 +1401,6 @@ def encode(self, variable: Variable, name: T_Name = None) -> Variable:
1401
1401
if "units" in encoding and not has_timedelta64_encoding_dtype (encoding ):
1402
1402
dtype = encoding .pop ("dtype" , None )
1403
1403
units = encoding .pop ("units" , None )
1404
-
1405
- # in the case of packed data we need to encode into
1406
- # float first, the correct dtype will be established
1407
- # via CFScaleOffsetCoder/CFMaskCoder
1408
- set_dtype_encoding = None
1409
- if "add_offset" in encoding or "scale_factor" in encoding :
1410
- set_dtype_encoding = dtype
1411
- dtype = data .dtype if data .dtype .kind == "f" else "float64"
1412
-
1413
- # retain dtype for packed data
1414
- if set_dtype_encoding is not None :
1415
- safe_setitem (encoding , "dtype" , set_dtype_encoding , name = name )
1416
1404
else :
1417
1405
resolution , _ = np .datetime_data (variable .dtype )
1418
1406
dtype = np .int64
@@ -1422,6 +1410,19 @@ def encode(self, variable: Variable, name: T_Name = None) -> Variable:
1422
1410
# Remove dtype encoding if it exists to prevent it from
1423
1411
# interfering downstream in NonStringCoder.
1424
1412
encoding .pop ("dtype" , None )
1413
+
1414
+ # in the case of packed data we need to encode into
1415
+ # float first, the correct dtype will be established
1416
+ # via CFScaleOffsetCoder/CFMaskCoder
1417
+ set_dtype_encoding = None
1418
+ if "add_offset" in encoding or "scale_factor" in encoding :
1419
+ set_dtype_encoding = dtype
1420
+ dtype = data .dtype if data .dtype .kind == "f" else "float64"
1421
+
1422
+ # retain dtype for packed data
1423
+ if set_dtype_encoding is not None :
1424
+ safe_setitem (encoding , "dtype" , set_dtype_encoding , name = name )
1425
+
1425
1426
data , units = encode_cf_timedelta (data , units , dtype )
1426
1427
safe_setitem (attrs , "units" , units , name = name )
1427
1428
return Variable (dims , data , attrs , encoding , fastpath = True )
0 commit comments