@@ -2757,18 +2757,34 @@ def test_align_str_dtype(self) -> None:
27572757 assert expected_b .x .dtype == actual_b .x .dtype
27582758
27592759 @pytest .mark .parametrize ("join" , ["left" , "override" ])
2760- def test_align_index_var_attrs (self , join ) -> None :
2761- # regression test https://github.com/pydata/xarray/issues/6852
2762- # aligning two objects should have no side effect on their index variable
2763- # metadata.
2764-
2765- ds = Dataset (coords = {"x" : ("x" , [1 , 2 , 3 ], {"units" : "m" })})
2766- ds_noattr = Dataset (coords = {"x" : ("x" , [1 , 2 , 3 ])})
2767-
2768- xr .align (ds_noattr , ds , join = join )
2769-
2770- assert ds .x .attrs == {"units" : "m" }
2771- assert ds_noattr .x .attrs == {}
2760+ @pytest .mark .parametrize ("as_dataset" , [False , True ])
2761+ def test_align_index_var_attrs (
2762+ self , join : Literal ["left" , "override" ], as_dataset : bool
2763+ ) -> None :
2764+ # Regression tests for GH6852 and GH6860:
2765+ # Aligning objects should have no side effect on their index variable
2766+ # metadata, and each result should retain its own metadata.
2767+
2768+ first = DataArray ([1 , 2 ], dims = "x" , coords = {"x" : [1.04 , 2.06 ]}, name = "a" )
2769+ first .x .attrs = {"units" : "m" }
2770+ first .x .encoding = {"dtype" : "int16" , "scale_factor" : 0.1 }
2771+ second = first .copy (deep = True )
2772+ second .x .attrs = {"units" : "km" }
2773+ second .x .encoding = {"dtype" : "float64" }
2774+ empty = first .copy (deep = True )
2775+ empty .x .attrs = {}
2776+ empty .x .encoding = {}
2777+ objects = [first , second , empty ]
2778+ inputs = [obj .to_dataset () for obj in objects ] if as_dataset else objects
2779+ originals = [obj .copy (deep = True ) for obj in inputs ]
2780+
2781+ results = xr .align (* inputs , join = join )
2782+
2783+ for original , source , result in zip (originals , inputs , results , strict = True ):
2784+ assert_identical (source , original )
2785+ assert source .x .encoding == original .x .encoding
2786+ assert_identical (result , original )
2787+ assert result .x .encoding == original .x .encoding
27722788
27732789 def test_align_scalar_index (self ) -> None :
27742790 # ensure that indexes associated with scalar coordinates are not ignored
0 commit comments