@@ -3869,6 +3869,27 @@ def test_zarr_fill_value_setting(self, dtype):
38693869 # ``raise_on_invalid=vn in check_encoding_set`` line in zarr.py
38703870 # ds.foo.encoding["fill_value"] = fv
38713871
3872+ def test_zarr_fill_value_in_encoding_on_read (self ) -> None :
3873+ # GH #10269: the Zarr array fill_value should be preserved in the
3874+ # variable encoding on read, so that it is not lost on round-trip.
3875+ # `fill_value` is an independent encoding key only for zarr_format 3;
3876+ # for zarr_format 2 the fill_value is set via `_FillValue`.
3877+ if not has_zarr_v3 or zarr .config .get ("default_zarr_format" ) != 3 :
3878+ pytest .skip ("fill_value is only an encoding key for zarr_format 3" )
3879+
3880+ ds = xr .Dataset ({"foo" : ("x" , [1 , 2 , 3 ])})
3881+ ds .foo .encoding = {"fill_value" : - 99 }
3882+
3883+ open_kwargs = {"consolidated" : False , "use_zarr_fill_value_as_mask" : False }
3884+ with self .roundtrip (ds , open_kwargs = open_kwargs ) as actual :
3885+ assert actual .foo .encoding ["fill_value" ] == - 99
3886+
3887+ # the fill_value must survive an open -> write -> open round-trip even
3888+ # when the user never touches the encoding explicitly
3889+ with self .roundtrip (ds , open_kwargs = open_kwargs ) as opened :
3890+ with self .roundtrip (opened , open_kwargs = open_kwargs ) as actual :
3891+ assert actual .foo .encoding ["fill_value" ] == - 99
3892+
38723893
38733894@requires_zarr
38743895@pytest .mark .skipif (
0 commit comments