1010 date_range ,
1111)
1212import pandas ._testing as tm
13+ from pandas .util .version import Version
1314
14- pytest .importorskip ("xarray" )
15+ xarray = pytest .importorskip ("xarray" )
1516
1617
1718class TestDataFrameToXArray :
@@ -30,13 +31,17 @@ def df(self):
3031 }
3132 )
3233
33- def test_to_xarray_index_types (self , index_flat , df , using_infer_string ):
34+ def test_to_xarray_index_types (self , index_flat , df , request ):
3435 index = index_flat
3536 # MultiIndex is tested in test_to_xarray_with_multiindex
3637 if len (index ) == 0 :
3738 pytest .skip ("Test doesn't make sense for empty index" )
38-
39- from xarray import Dataset
39+ elif Version (xarray .__version__ ) <= Version ("2024.9.0" ):
40+ request .applymarker (
41+ pytest .mark .xfail (
42+ reason = "Categorical column not preserved." ,
43+ )
44+ )
4045
4146 df .index = index [:4 ]
4247 df .index .name = "foo"
@@ -46,7 +51,7 @@ def test_to_xarray_index_types(self, index_flat, df, using_infer_string):
4651 assert len (result .coords ) == 1
4752 assert len (result .data_vars ) == 8
4853 tm .assert_almost_equal (list (result .coords .keys ()), ["foo" ])
49- assert isinstance (result , Dataset )
54+ assert isinstance (result , xarray . Dataset )
5055
5156 # idempotency
5257 # datetimes w/tz are preserved
@@ -56,16 +61,12 @@ def test_to_xarray_index_types(self, index_flat, df, using_infer_string):
5661 tm .assert_frame_equal (result .to_dataframe (), expected )
5762
5863 def test_to_xarray_empty (self , df ):
59- from xarray import Dataset
60-
6164 df .index .name = "foo"
6265 result = df [0 :0 ].to_xarray ()
6366 assert result .sizes ["foo" ] == 0
64- assert isinstance (result , Dataset )
67+ assert isinstance (result , xarray . Dataset )
6568
6669 def test_to_xarray_with_multiindex (self , df , using_infer_string ):
67- from xarray import Dataset
68-
6970 # MultiIndex
7071 df .index = MultiIndex .from_product ([["a" ], range (4 )], names = ["one" , "two" ])
7172 result = df .to_xarray ()
@@ -74,7 +75,7 @@ def test_to_xarray_with_multiindex(self, df, using_infer_string):
7475 assert len (result .coords ) == 2
7576 assert len (result .data_vars ) == 8
7677 tm .assert_almost_equal (list (result .coords .keys ()), ["one" , "two" ])
77- assert isinstance (result , Dataset )
78+ assert isinstance (result , xarray . Dataset )
7879
7980 result = result .to_dataframe ()
8081 expected = df .copy ()
@@ -88,7 +89,11 @@ def test_to_xarray_with_multiindex(self, df, using_infer_string):
8889class TestSeriesToXArray :
8990 def test_to_xarray_index_types (self , index_flat , request ):
9091 index = index_flat
91- if isinstance (index .dtype , StringDtype ) and index .dtype .storage == "pyarrow" :
92+ if (
93+ isinstance (index .dtype , StringDtype )
94+ and index .dtype .storage == "pyarrow"
95+ and Version (xarray .__version__ ) > Version ("2024.9.0" )
96+ ):
9297 request .applymarker (
9398 pytest .mark .xfail (
9499 reason = "xarray calling reshape of ArrowExtensionArray" ,
@@ -97,39 +102,33 @@ def test_to_xarray_index_types(self, index_flat, request):
97102 )
98103 # MultiIndex is tested in test_to_xarray_with_multiindex
99104
100- from xarray import DataArray
101-
102105 ser = Series (range (len (index )), index = index , dtype = "int64" )
103106 ser .index .name = "foo"
104107 result = ser .to_xarray ()
105108 repr (result )
106109 assert len (result ) == len (index )
107110 assert len (result .coords ) == 1
108111 tm .assert_almost_equal (list (result .coords .keys ()), ["foo" ])
109- assert isinstance (result , DataArray )
112+ assert isinstance (result , xarray . DataArray )
110113
111114 # idempotency
112115 tm .assert_series_equal (result .to_series (), ser )
113116
114117 def test_to_xarray_empty (self ):
115- from xarray import DataArray
116-
117118 ser = Series ([], dtype = object )
118119 ser .index .name = "foo"
119120 result = ser .to_xarray ()
120121 assert len (result ) == 0
121122 assert len (result .coords ) == 1
122123 tm .assert_almost_equal (list (result .coords .keys ()), ["foo" ])
123- assert isinstance (result , DataArray )
124+ assert isinstance (result , xarray . DataArray )
124125
125126 def test_to_xarray_with_multiindex (self ):
126- from xarray import DataArray
127-
128127 mi = MultiIndex .from_product ([["a" , "b" ], range (3 )], names = ["one" , "two" ])
129128 ser = Series (range (6 ), dtype = "int64" , index = mi )
130129 result = ser .to_xarray ()
131130 assert len (result ) == 2
132131 tm .assert_almost_equal (list (result .coords .keys ()), ["one" , "two" ])
133- assert isinstance (result , DataArray )
132+ assert isinstance (result , xarray . DataArray )
134133 res = result .to_series ()
135134 tm .assert_series_equal (res , ser )
0 commit comments