@@ -1355,20 +1355,15 @@ def encode(self, variable: Variable, name: T_Name = None) -> Variable:
1355
1355
1356
1356
units = encoding .pop ("units" , None )
1357
1357
calendar = encoding .pop ("calendar" , None )
1358
- dtype = encoding .pop ("dtype" , None )
1358
+ dtype = encoding .get ("dtype" , None )
1359
1359
1360
1360
# in the case of packed data we need to encode into
1361
1361
# float first, the correct dtype will be established
1362
1362
# via CFScaleOffsetCoder/CFMaskCoder
1363
- set_dtype_encoding = None
1364
1363
if "add_offset" in encoding or "scale_factor" in encoding :
1365
- set_dtype_encoding = dtype
1366
1364
dtype = data .dtype if data .dtype .kind == "f" else "float64"
1367
1365
(data , units , calendar ) = encode_cf_datetime (data , units , calendar , dtype )
1368
1366
1369
- # retain dtype for packed data
1370
- if set_dtype_encoding is not None :
1371
- safe_setitem (encoding , "dtype" , set_dtype_encoding , name = name )
1372
1367
safe_setitem (attrs , "units" , units , name = name )
1373
1368
safe_setitem (attrs , "calendar" , calendar , name = name )
1374
1369
@@ -1420,22 +1415,16 @@ def encode(self, variable: Variable, name: T_Name = None) -> Variable:
1420
1415
if np .issubdtype (variable .data .dtype , np .timedelta64 ):
1421
1416
dims , data , attrs , encoding = unpack_for_encoding (variable )
1422
1417
1423
- dtype = encoding .pop ("dtype" , None )
1418
+ dtype = encoding .get ("dtype" , None )
1424
1419
1425
1420
# in the case of packed data we need to encode into
1426
1421
# float first, the correct dtype will be established
1427
1422
# via CFScaleOffsetCoder/CFMaskCoder
1428
- set_dtype_encoding = None
1429
1423
if "add_offset" in encoding or "scale_factor" in encoding :
1430
- set_dtype_encoding = dtype
1431
1424
dtype = data .dtype if data .dtype .kind == "f" else "float64"
1432
1425
1433
1426
data , units = encode_cf_timedelta (data , encoding .pop ("units" , None ), dtype )
1434
1427
1435
- # retain dtype for packed data
1436
- if set_dtype_encoding is not None :
1437
- safe_setitem (encoding , "dtype" , set_dtype_encoding , name = name )
1438
-
1439
1428
safe_setitem (attrs , "units" , units , name = name )
1440
1429
1441
1430
return Variable (dims , data , attrs , encoding , fastpath = True )
0 commit comments