@@ -28,12 +28,15 @@ def test_constructor(self):
28
28
assert index .dim == "x"
29
29
30
30
def test_from_variables (self ):
31
+ # pandas has only Float64Index but variable dtype should be preserved
32
+ data = np .array ([1.1 , 2.2 , 3.3 ], dtype = np .float32 )
31
33
var = xr .Variable (
32
- "x" , [ 1 , 2 , 3 ], attrs = {"unit" : "m" }, encoding = {"dtype" : np .int32 }
34
+ "x" , data , attrs = {"unit" : "m" }, encoding = {"dtype" : np .float64 }
33
35
)
34
36
35
37
index , index_vars = PandasIndex .from_variables ({"x" : var })
36
38
xr .testing .assert_identical (var .to_index_variable (), index_vars ["x" ])
39
+ assert index_vars ["x" ].dtype == var .dtype
37
40
assert index .dim == "x"
38
41
assert index .index .equals (index_vars ["x" ].to_index ())
39
42
@@ -166,16 +169,20 @@ def test_from_variables(self):
166
169
PandasMultiIndex .from_variables ({"level1" : v_level1 , "level3" : v_level3 })
167
170
168
171
def test_from_pandas_index (self ):
169
- pd_idx = pd .MultiIndex .from_arrays ([[1 , 2 , 3 ], [4 , 5 , 6 ]], names = ("foo" , "bar" ))
172
+ foo_data = np .array ([0 , 0 , 1 ], dtype = "int" )
173
+ bar_data = np .array ([1.1 , 1.2 , 1.3 ], dtype = "float64" )
174
+ pd_idx = pd .MultiIndex .from_arrays ([foo_data , bar_data ], names = ("foo" , "bar" ))
170
175
171
176
index , index_vars = PandasMultiIndex .from_pandas_index (pd_idx , "x" )
172
177
173
178
assert index .dim == "x"
174
- assert index .index is pd_idx
179
+ assert index .index . equals ( pd_idx )
175
180
assert index .index .names == ("foo" , "bar" )
176
181
xr .testing .assert_identical (index_vars ["x" ], IndexVariable ("x" , pd_idx ))
177
- xr .testing .assert_identical (index_vars ["foo" ], IndexVariable ("x" , [1 , 2 , 3 ]))
178
- xr .testing .assert_identical (index_vars ["bar" ], IndexVariable ("x" , [4 , 5 , 6 ]))
182
+ xr .testing .assert_identical (index_vars ["foo" ], IndexVariable ("x" , foo_data ))
183
+ xr .testing .assert_identical (index_vars ["bar" ], IndexVariable ("x" , bar_data ))
184
+ assert index_vars ["foo" ].dtype == foo_data .dtype
185
+ assert index_vars ["bar" ].dtype == bar_data .dtype
179
186
180
187
def test_query (self ):
181
188
index = PandasMultiIndex (
0 commit comments