10
10
date_range ,
11
11
)
12
12
import pandas ._testing as tm
13
+ from pandas .util .version import Version
13
14
14
- pytest .importorskip ("xarray" )
15
+ xarray = pytest .importorskip ("xarray" )
15
16
16
17
17
18
class TestDataFrameToXArray :
@@ -30,13 +31,17 @@ def df(self):
30
31
}
31
32
)
32
33
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 ):
34
35
index = index_flat
35
36
# MultiIndex is tested in test_to_xarray_with_multiindex
36
37
if len (index ) == 0 :
37
38
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
+ )
40
45
41
46
df .index = index [:4 ]
42
47
df .index .name = "foo"
@@ -46,7 +51,7 @@ def test_to_xarray_index_types(self, index_flat, df, using_infer_string):
46
51
assert len (result .coords ) == 1
47
52
assert len (result .data_vars ) == 8
48
53
tm .assert_almost_equal (list (result .coords .keys ()), ["foo" ])
49
- assert isinstance (result , Dataset )
54
+ assert isinstance (result , xarray . Dataset )
50
55
51
56
# idempotency
52
57
# datetimes w/tz are preserved
@@ -56,16 +61,12 @@ def test_to_xarray_index_types(self, index_flat, df, using_infer_string):
56
61
tm .assert_frame_equal (result .to_dataframe (), expected )
57
62
58
63
def test_to_xarray_empty (self , df ):
59
- from xarray import Dataset
60
-
61
64
df .index .name = "foo"
62
65
result = df [0 :0 ].to_xarray ()
63
66
assert result .sizes ["foo" ] == 0
64
- assert isinstance (result , Dataset )
67
+ assert isinstance (result , xarray . Dataset )
65
68
66
69
def test_to_xarray_with_multiindex (self , df , using_infer_string ):
67
- from xarray import Dataset
68
-
69
70
# MultiIndex
70
71
df .index = MultiIndex .from_product ([["a" ], range (4 )], names = ["one" , "two" ])
71
72
result = df .to_xarray ()
@@ -74,7 +75,7 @@ def test_to_xarray_with_multiindex(self, df, using_infer_string):
74
75
assert len (result .coords ) == 2
75
76
assert len (result .data_vars ) == 8
76
77
tm .assert_almost_equal (list (result .coords .keys ()), ["one" , "two" ])
77
- assert isinstance (result , Dataset )
78
+ assert isinstance (result , xarray . Dataset )
78
79
79
80
result = result .to_dataframe ()
80
81
expected = df .copy ()
@@ -88,7 +89,11 @@ def test_to_xarray_with_multiindex(self, df, using_infer_string):
88
89
class TestSeriesToXArray :
89
90
def test_to_xarray_index_types (self , index_flat , request ):
90
91
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
+ ):
92
97
request .applymarker (
93
98
pytest .mark .xfail (
94
99
reason = "xarray calling reshape of ArrowExtensionArray" ,
@@ -97,39 +102,33 @@ def test_to_xarray_index_types(self, index_flat, request):
97
102
)
98
103
# MultiIndex is tested in test_to_xarray_with_multiindex
99
104
100
- from xarray import DataArray
101
-
102
105
ser = Series (range (len (index )), index = index , dtype = "int64" )
103
106
ser .index .name = "foo"
104
107
result = ser .to_xarray ()
105
108
repr (result )
106
109
assert len (result ) == len (index )
107
110
assert len (result .coords ) == 1
108
111
tm .assert_almost_equal (list (result .coords .keys ()), ["foo" ])
109
- assert isinstance (result , DataArray )
112
+ assert isinstance (result , xarray . DataArray )
110
113
111
114
# idempotency
112
115
tm .assert_series_equal (result .to_series (), ser )
113
116
114
117
def test_to_xarray_empty (self ):
115
- from xarray import DataArray
116
-
117
118
ser = Series ([], dtype = object )
118
119
ser .index .name = "foo"
119
120
result = ser .to_xarray ()
120
121
assert len (result ) == 0
121
122
assert len (result .coords ) == 1
122
123
tm .assert_almost_equal (list (result .coords .keys ()), ["foo" ])
123
- assert isinstance (result , DataArray )
124
+ assert isinstance (result , xarray . DataArray )
124
125
125
126
def test_to_xarray_with_multiindex (self ):
126
- from xarray import DataArray
127
-
128
127
mi = MultiIndex .from_product ([["a" , "b" ], range (3 )], names = ["one" , "two" ])
129
128
ser = Series (range (6 ), dtype = "int64" , index = mi )
130
129
result = ser .to_xarray ()
131
130
assert len (result ) == 2
132
131
tm .assert_almost_equal (list (result .coords .keys ()), ["one" , "two" ])
133
- assert isinstance (result , DataArray )
132
+ assert isinstance (result , xarray . DataArray )
134
133
res = result .to_series ()
135
134
tm .assert_series_equal (res , ser )
0 commit comments