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