Skip to content

Commit b257914

Browse files
jbrockmendelSeeminSyed
authored andcommitted
REF: collect to_xarray tests (pandas-dev#32877)
* implement test_to_xarray * dummy commit to force CI
1 parent 63fb381 commit b257914

File tree

3 files changed

+154
-164
lines changed

3 files changed

+154
-164
lines changed

pandas/tests/generic/test_frame.py

-100
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
11
from copy import deepcopy
2-
from distutils.version import LooseVersion
32
from operator import methodcaller
43

54
import numpy as np
65
import pytest
76

8-
import pandas.util._test_decorators as td
9-
10-
from pandas.core.dtypes.generic import ABCMultiIndex
11-
127
import pandas as pd
138
from pandas import DataFrame, MultiIndex, Series, date_range
149
import pandas._testing as tm
1510

1611
from .test_generic import Generic
1712

18-
try:
19-
import xarray
20-
21-
_XARRAY_INSTALLED = True
22-
except ImportError:
23-
_XARRAY_INSTALLED = False
24-
2513

2614
class TestDataFrame(Generic):
2715
_typ = DataFrame
@@ -238,91 +226,3 @@ def test_unexpected_keyword(self):
238226

239227
with pytest.raises(TypeError, match=msg):
240228
ts.fillna(0, in_place=True)
241-
242-
243-
class TestToXArray:
244-
@pytest.mark.skipif(
245-
not _XARRAY_INSTALLED
246-
or _XARRAY_INSTALLED
247-
and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"),
248-
reason="xarray >= 0.10.0 required",
249-
)
250-
def test_to_xarray_index_types(self, indices):
251-
if isinstance(indices, ABCMultiIndex):
252-
pytest.skip("MultiIndex is tested separately")
253-
if len(indices) == 0:
254-
pytest.skip("Test doesn't make sense for empty index")
255-
256-
from xarray import Dataset
257-
258-
df = DataFrame(
259-
{
260-
"a": list("abc"),
261-
"b": list(range(1, 4)),
262-
"c": np.arange(3, 6).astype("u1"),
263-
"d": np.arange(4.0, 7.0, dtype="float64"),
264-
"e": [True, False, True],
265-
"f": pd.Categorical(list("abc")),
266-
"g": pd.date_range("20130101", periods=3),
267-
"h": pd.date_range("20130101", periods=3, tz="US/Eastern"),
268-
}
269-
)
270-
271-
df.index = indices[:3]
272-
df.index.name = "foo"
273-
df.columns.name = "bar"
274-
result = df.to_xarray()
275-
assert result.dims["foo"] == 3
276-
assert len(result.coords) == 1
277-
assert len(result.data_vars) == 8
278-
tm.assert_almost_equal(list(result.coords.keys()), ["foo"])
279-
assert isinstance(result, Dataset)
280-
281-
# idempotency
282-
# datetimes w/tz are preserved
283-
# column names are lost
284-
expected = df.copy()
285-
expected["f"] = expected["f"].astype(object)
286-
expected.columns.name = None
287-
tm.assert_frame_equal(
288-
result.to_dataframe(), expected,
289-
)
290-
291-
@td.skip_if_no("xarray", min_version="0.7.0")
292-
def test_to_xarray(self):
293-
from xarray import Dataset
294-
295-
df = DataFrame(
296-
{
297-
"a": list("abc"),
298-
"b": list(range(1, 4)),
299-
"c": np.arange(3, 6).astype("u1"),
300-
"d": np.arange(4.0, 7.0, dtype="float64"),
301-
"e": [True, False, True],
302-
"f": pd.Categorical(list("abc")),
303-
"g": pd.date_range("20130101", periods=3),
304-
"h": pd.date_range("20130101", periods=3, tz="US/Eastern"),
305-
}
306-
)
307-
308-
df.index.name = "foo"
309-
result = df[0:0].to_xarray()
310-
assert result.dims["foo"] == 0
311-
assert isinstance(result, Dataset)
312-
313-
# available in 0.7.1
314-
# MultiIndex
315-
df.index = pd.MultiIndex.from_product([["a"], range(3)], names=["one", "two"])
316-
result = df.to_xarray()
317-
assert result.dims["one"] == 1
318-
assert result.dims["two"] == 3
319-
assert len(result.coords) == 2
320-
assert len(result.data_vars) == 8
321-
tm.assert_almost_equal(list(result.coords.keys()), ["one", "two"])
322-
assert isinstance(result, Dataset)
323-
324-
result = result.to_dataframe()
325-
expected = df.copy()
326-
expected["f"] = expected["f"].astype(object)
327-
expected.columns.name = None
328-
tm.assert_frame_equal(result, expected, check_index_type=False)

pandas/tests/generic/test_series.py

-64
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
1-
from distutils.version import LooseVersion
21
from operator import methodcaller
32

43
import numpy as np
54
import pytest
65

7-
import pandas.util._test_decorators as td
8-
96
import pandas as pd
107
from pandas import MultiIndex, Series, date_range
118
import pandas._testing as tm
129

13-
from ...core.dtypes.generic import ABCMultiIndex
1410
from .test_generic import Generic
1511

16-
try:
17-
import xarray
18-
19-
_XARRAY_INSTALLED = True
20-
except ImportError:
21-
_XARRAY_INSTALLED = False
22-
2312

2413
class TestSeries(Generic):
2514
_typ = Series
@@ -199,56 +188,3 @@ def test_datetime_shift_always_copy(self, move_by_freq):
199188
# GH22397
200189
s = pd.Series(range(5), index=pd.date_range("2017", periods=5))
201190
assert s.shift(freq=move_by_freq) is not s
202-
203-
204-
class TestToXArray:
205-
@pytest.mark.skipif(
206-
not _XARRAY_INSTALLED
207-
or _XARRAY_INSTALLED
208-
and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"),
209-
reason="xarray >= 0.10.0 required",
210-
)
211-
def test_to_xarray_index_types(self, indices):
212-
if isinstance(indices, ABCMultiIndex):
213-
pytest.skip("MultiIndex is tested separately")
214-
215-
from xarray import DataArray
216-
217-
s = Series(range(len(indices)), index=indices, dtype="object")
218-
s.index.name = "foo"
219-
result = s.to_xarray()
220-
repr(result)
221-
assert len(result) == len(indices)
222-
assert len(result.coords) == 1
223-
tm.assert_almost_equal(list(result.coords.keys()), ["foo"])
224-
assert isinstance(result, DataArray)
225-
226-
# idempotency
227-
tm.assert_series_equal(result.to_series(), s, check_index_type=False)
228-
229-
@td.skip_if_no("xarray", min_version="0.7.0")
230-
def test_to_xarray_multiindex(self):
231-
from xarray import DataArray
232-
233-
s = Series(range(6))
234-
s.index.name = "foo"
235-
s.index = pd.MultiIndex.from_product(
236-
[["a", "b"], range(3)], names=["one", "two"]
237-
)
238-
result = s.to_xarray()
239-
assert len(result) == 2
240-
tm.assert_almost_equal(list(result.coords.keys()), ["one", "two"])
241-
assert isinstance(result, DataArray)
242-
tm.assert_series_equal(result.to_series(), s)
243-
244-
@td.skip_if_no("xarray", min_version="0.7.0")
245-
def test_to_xarray(self):
246-
from xarray import DataArray
247-
248-
s = Series([], dtype=object)
249-
s.index.name = "foo"
250-
result = s.to_xarray()
251-
assert len(result) == 0
252-
assert len(result.coords) == 1
253-
tm.assert_almost_equal(list(result.coords.keys()), ["foo"])
254-
assert isinstance(result, DataArray)
+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
from distutils.version import LooseVersion
2+
3+
import numpy as np
4+
import pytest
5+
6+
import pandas.util._test_decorators as td
7+
8+
import pandas as pd
9+
from pandas import DataFrame, Series
10+
import pandas._testing as tm
11+
12+
try:
13+
import xarray
14+
15+
_XARRAY_INSTALLED = True
16+
except ImportError:
17+
_XARRAY_INSTALLED = False
18+
19+
20+
class TestDataFrameToXArray:
21+
@pytest.mark.skipif(
22+
not _XARRAY_INSTALLED
23+
or _XARRAY_INSTALLED
24+
and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"),
25+
reason="xarray >= 0.10.0 required",
26+
)
27+
def test_to_xarray_index_types(self, indices):
28+
if isinstance(indices, pd.MultiIndex):
29+
pytest.skip("MultiIndex is tested separately")
30+
if len(indices) == 0:
31+
pytest.skip("Test doesn't make sense for empty index")
32+
33+
from xarray import Dataset
34+
35+
df = DataFrame(
36+
{
37+
"a": list("abc"),
38+
"b": list(range(1, 4)),
39+
"c": np.arange(3, 6).astype("u1"),
40+
"d": np.arange(4.0, 7.0, dtype="float64"),
41+
"e": [True, False, True],
42+
"f": pd.Categorical(list("abc")),
43+
"g": pd.date_range("20130101", periods=3),
44+
"h": pd.date_range("20130101", periods=3, tz="US/Eastern"),
45+
}
46+
)
47+
48+
df.index = indices[:3]
49+
df.index.name = "foo"
50+
df.columns.name = "bar"
51+
result = df.to_xarray()
52+
assert result.dims["foo"] == 3
53+
assert len(result.coords) == 1
54+
assert len(result.data_vars) == 8
55+
tm.assert_almost_equal(list(result.coords.keys()), ["foo"])
56+
assert isinstance(result, Dataset)
57+
58+
# idempotency
59+
# datetimes w/tz are preserved
60+
# column names are lost
61+
expected = df.copy()
62+
expected["f"] = expected["f"].astype(object)
63+
expected.columns.name = None
64+
tm.assert_frame_equal(
65+
result.to_dataframe(), expected,
66+
)
67+
68+
@td.skip_if_no("xarray", min_version="0.7.0")
69+
def test_to_xarray(self):
70+
from xarray import Dataset
71+
72+
df = DataFrame(
73+
{
74+
"a": list("abc"),
75+
"b": list(range(1, 4)),
76+
"c": np.arange(3, 6).astype("u1"),
77+
"d": np.arange(4.0, 7.0, dtype="float64"),
78+
"e": [True, False, True],
79+
"f": pd.Categorical(list("abc")),
80+
"g": pd.date_range("20130101", periods=3),
81+
"h": pd.date_range("20130101", periods=3, tz="US/Eastern"),
82+
}
83+
)
84+
85+
df.index.name = "foo"
86+
result = df[0:0].to_xarray()
87+
assert result.dims["foo"] == 0
88+
assert isinstance(result, Dataset)
89+
90+
# available in 0.7.1
91+
# MultiIndex
92+
df.index = pd.MultiIndex.from_product([["a"], range(3)], names=["one", "two"])
93+
result = df.to_xarray()
94+
assert result.dims["one"] == 1
95+
assert result.dims["two"] == 3
96+
assert len(result.coords) == 2
97+
assert len(result.data_vars) == 8
98+
tm.assert_almost_equal(list(result.coords.keys()), ["one", "two"])
99+
assert isinstance(result, Dataset)
100+
101+
result = result.to_dataframe()
102+
expected = df.copy()
103+
expected["f"] = expected["f"].astype(object)
104+
expected.columns.name = None
105+
tm.assert_frame_equal(result, expected, check_index_type=False)
106+
107+
108+
class TestSeriesToXArray:
109+
@pytest.mark.skipif(
110+
not _XARRAY_INSTALLED
111+
or _XARRAY_INSTALLED
112+
and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"),
113+
reason="xarray >= 0.10.0 required",
114+
)
115+
def test_to_xarray_index_types(self, indices):
116+
if isinstance(indices, pd.MultiIndex):
117+
pytest.skip("MultiIndex is tested separately")
118+
119+
from xarray import DataArray
120+
121+
s = Series(range(len(indices)), index=indices)
122+
s.index.name = "foo"
123+
result = s.to_xarray()
124+
repr(result)
125+
assert len(result) == len(indices)
126+
assert len(result.coords) == 1
127+
tm.assert_almost_equal(list(result.coords.keys()), ["foo"])
128+
assert isinstance(result, DataArray)
129+
130+
# idempotency
131+
tm.assert_series_equal(result.to_series(), s, check_index_type=False)
132+
133+
@td.skip_if_no("xarray", min_version="0.7.0")
134+
def test_to_xarray(self):
135+
from xarray import DataArray
136+
137+
s = Series([], dtype=object)
138+
s.index.name = "foo"
139+
result = s.to_xarray()
140+
assert len(result) == 0
141+
assert len(result.coords) == 1
142+
tm.assert_almost_equal(list(result.coords.keys()), ["foo"])
143+
assert isinstance(result, DataArray)
144+
145+
s = Series(range(6))
146+
s.index.name = "foo"
147+
s.index = pd.MultiIndex.from_product(
148+
[["a", "b"], range(3)], names=["one", "two"]
149+
)
150+
result = s.to_xarray()
151+
assert len(result) == 2
152+
tm.assert_almost_equal(list(result.coords.keys()), ["one", "two"])
153+
assert isinstance(result, DataArray)
154+
tm.assert_series_equal(result.to_series(), s)

0 commit comments

Comments
 (0)