diff --git a/pandas/tests/generic/test_frame.py b/pandas/tests/generic/test_frame.py index 178782c299483..31501f20db453 100644 --- a/pandas/tests/generic/test_frame.py +++ b/pandas/tests/generic/test_frame.py @@ -1,27 +1,15 @@ from copy import deepcopy -from distutils.version import LooseVersion from operator import methodcaller import numpy as np import pytest -import pandas.util._test_decorators as td - -from pandas.core.dtypes.generic import ABCMultiIndex - import pandas as pd from pandas import DataFrame, MultiIndex, Series, date_range import pandas._testing as tm from .test_generic import Generic -try: - import xarray - - _XARRAY_INSTALLED = True -except ImportError: - _XARRAY_INSTALLED = False - class TestDataFrame(Generic): _typ = DataFrame @@ -238,91 +226,3 @@ def test_unexpected_keyword(self): with pytest.raises(TypeError, match=msg): ts.fillna(0, in_place=True) - - -class TestToXArray: - @pytest.mark.skipif( - not _XARRAY_INSTALLED - or _XARRAY_INSTALLED - and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"), - reason="xarray >= 0.10.0 required", - ) - def test_to_xarray_index_types(self, indices): - if isinstance(indices, ABCMultiIndex): - pytest.skip("MultiIndex is tested separately") - if len(indices) == 0: - pytest.skip("Test doesn't make sense for empty index") - - from xarray import Dataset - - df = DataFrame( - { - "a": list("abc"), - "b": list(range(1, 4)), - "c": np.arange(3, 6).astype("u1"), - "d": np.arange(4.0, 7.0, dtype="float64"), - "e": [True, False, True], - "f": pd.Categorical(list("abc")), - "g": pd.date_range("20130101", periods=3), - "h": pd.date_range("20130101", periods=3, tz="US/Eastern"), - } - ) - - df.index = indices[:3] - df.index.name = "foo" - df.columns.name = "bar" - result = df.to_xarray() - assert result.dims["foo"] == 3 - assert len(result.coords) == 1 - assert len(result.data_vars) == 8 - tm.assert_almost_equal(list(result.coords.keys()), ["foo"]) - assert isinstance(result, Dataset) - - # idempotency - # datetimes w/tz are preserved - # column names are lost - expected = df.copy() - expected["f"] = expected["f"].astype(object) - expected.columns.name = None - tm.assert_frame_equal( - result.to_dataframe(), expected, - ) - - @td.skip_if_no("xarray", min_version="0.7.0") - def test_to_xarray(self): - from xarray import Dataset - - df = DataFrame( - { - "a": list("abc"), - "b": list(range(1, 4)), - "c": np.arange(3, 6).astype("u1"), - "d": np.arange(4.0, 7.0, dtype="float64"), - "e": [True, False, True], - "f": pd.Categorical(list("abc")), - "g": pd.date_range("20130101", periods=3), - "h": pd.date_range("20130101", periods=3, tz="US/Eastern"), - } - ) - - df.index.name = "foo" - result = df[0:0].to_xarray() - assert result.dims["foo"] == 0 - assert isinstance(result, Dataset) - - # available in 0.7.1 - # MultiIndex - df.index = pd.MultiIndex.from_product([["a"], range(3)], names=["one", "two"]) - result = df.to_xarray() - assert result.dims["one"] == 1 - assert result.dims["two"] == 3 - assert len(result.coords) == 2 - assert len(result.data_vars) == 8 - tm.assert_almost_equal(list(result.coords.keys()), ["one", "two"]) - assert isinstance(result, Dataset) - - result = result.to_dataframe() - expected = df.copy() - expected["f"] = expected["f"].astype(object) - expected.columns.name = None - tm.assert_frame_equal(result, expected, check_index_type=False) diff --git a/pandas/tests/generic/test_series.py b/pandas/tests/generic/test_series.py index 8fdc8381eef78..20f6cda7cad60 100644 --- a/pandas/tests/generic/test_series.py +++ b/pandas/tests/generic/test_series.py @@ -1,25 +1,14 @@ -from distutils.version import LooseVersion from operator import methodcaller import numpy as np import pytest -import pandas.util._test_decorators as td - import pandas as pd from pandas import MultiIndex, Series, date_range import pandas._testing as tm -from ...core.dtypes.generic import ABCMultiIndex from .test_generic import Generic -try: - import xarray - - _XARRAY_INSTALLED = True -except ImportError: - _XARRAY_INSTALLED = False - class TestSeries(Generic): _typ = Series @@ -199,56 +188,3 @@ def test_datetime_shift_always_copy(self, move_by_freq): # GH22397 s = pd.Series(range(5), index=pd.date_range("2017", periods=5)) assert s.shift(freq=move_by_freq) is not s - - -class TestToXArray: - @pytest.mark.skipif( - not _XARRAY_INSTALLED - or _XARRAY_INSTALLED - and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"), - reason="xarray >= 0.10.0 required", - ) - def test_to_xarray_index_types(self, indices): - if isinstance(indices, ABCMultiIndex): - pytest.skip("MultiIndex is tested separately") - - from xarray import DataArray - - s = Series(range(len(indices)), index=indices, dtype="object") - s.index.name = "foo" - result = s.to_xarray() - repr(result) - assert len(result) == len(indices) - assert len(result.coords) == 1 - tm.assert_almost_equal(list(result.coords.keys()), ["foo"]) - assert isinstance(result, DataArray) - - # idempotency - tm.assert_series_equal(result.to_series(), s, check_index_type=False) - - @td.skip_if_no("xarray", min_version="0.7.0") - def test_to_xarray_multiindex(self): - from xarray import DataArray - - s = Series(range(6)) - s.index.name = "foo" - s.index = pd.MultiIndex.from_product( - [["a", "b"], range(3)], names=["one", "two"] - ) - result = s.to_xarray() - assert len(result) == 2 - tm.assert_almost_equal(list(result.coords.keys()), ["one", "two"]) - assert isinstance(result, DataArray) - tm.assert_series_equal(result.to_series(), s) - - @td.skip_if_no("xarray", min_version="0.7.0") - def test_to_xarray(self): - from xarray import DataArray - - s = Series([], dtype=object) - s.index.name = "foo" - result = s.to_xarray() - assert len(result) == 0 - assert len(result.coords) == 1 - tm.assert_almost_equal(list(result.coords.keys()), ["foo"]) - assert isinstance(result, DataArray) diff --git a/pandas/tests/generic/test_to_xarray.py b/pandas/tests/generic/test_to_xarray.py new file mode 100644 index 0000000000000..250fe950a05fc --- /dev/null +++ b/pandas/tests/generic/test_to_xarray.py @@ -0,0 +1,154 @@ +from distutils.version import LooseVersion + +import numpy as np +import pytest + +import pandas.util._test_decorators as td + +import pandas as pd +from pandas import DataFrame, Series +import pandas._testing as tm + +try: + import xarray + + _XARRAY_INSTALLED = True +except ImportError: + _XARRAY_INSTALLED = False + + +class TestDataFrameToXArray: + @pytest.mark.skipif( + not _XARRAY_INSTALLED + or _XARRAY_INSTALLED + and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"), + reason="xarray >= 0.10.0 required", + ) + def test_to_xarray_index_types(self, indices): + if isinstance(indices, pd.MultiIndex): + pytest.skip("MultiIndex is tested separately") + if len(indices) == 0: + pytest.skip("Test doesn't make sense for empty index") + + from xarray import Dataset + + df = DataFrame( + { + "a": list("abc"), + "b": list(range(1, 4)), + "c": np.arange(3, 6).astype("u1"), + "d": np.arange(4.0, 7.0, dtype="float64"), + "e": [True, False, True], + "f": pd.Categorical(list("abc")), + "g": pd.date_range("20130101", periods=3), + "h": pd.date_range("20130101", periods=3, tz="US/Eastern"), + } + ) + + df.index = indices[:3] + df.index.name = "foo" + df.columns.name = "bar" + result = df.to_xarray() + assert result.dims["foo"] == 3 + assert len(result.coords) == 1 + assert len(result.data_vars) == 8 + tm.assert_almost_equal(list(result.coords.keys()), ["foo"]) + assert isinstance(result, Dataset) + + # idempotency + # datetimes w/tz are preserved + # column names are lost + expected = df.copy() + expected["f"] = expected["f"].astype(object) + expected.columns.name = None + tm.assert_frame_equal( + result.to_dataframe(), expected, + ) + + @td.skip_if_no("xarray", min_version="0.7.0") + def test_to_xarray(self): + from xarray import Dataset + + df = DataFrame( + { + "a": list("abc"), + "b": list(range(1, 4)), + "c": np.arange(3, 6).astype("u1"), + "d": np.arange(4.0, 7.0, dtype="float64"), + "e": [True, False, True], + "f": pd.Categorical(list("abc")), + "g": pd.date_range("20130101", periods=3), + "h": pd.date_range("20130101", periods=3, tz="US/Eastern"), + } + ) + + df.index.name = "foo" + result = df[0:0].to_xarray() + assert result.dims["foo"] == 0 + assert isinstance(result, Dataset) + + # available in 0.7.1 + # MultiIndex + df.index = pd.MultiIndex.from_product([["a"], range(3)], names=["one", "two"]) + result = df.to_xarray() + assert result.dims["one"] == 1 + assert result.dims["two"] == 3 + assert len(result.coords) == 2 + assert len(result.data_vars) == 8 + tm.assert_almost_equal(list(result.coords.keys()), ["one", "two"]) + assert isinstance(result, Dataset) + + result = result.to_dataframe() + expected = df.copy() + expected["f"] = expected["f"].astype(object) + expected.columns.name = None + tm.assert_frame_equal(result, expected, check_index_type=False) + + +class TestSeriesToXArray: + @pytest.mark.skipif( + not _XARRAY_INSTALLED + or _XARRAY_INSTALLED + and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"), + reason="xarray >= 0.10.0 required", + ) + def test_to_xarray_index_types(self, indices): + if isinstance(indices, pd.MultiIndex): + pytest.skip("MultiIndex is tested separately") + + from xarray import DataArray + + s = Series(range(len(indices)), index=indices) + s.index.name = "foo" + result = s.to_xarray() + repr(result) + assert len(result) == len(indices) + assert len(result.coords) == 1 + tm.assert_almost_equal(list(result.coords.keys()), ["foo"]) + assert isinstance(result, DataArray) + + # idempotency + tm.assert_series_equal(result.to_series(), s, check_index_type=False) + + @td.skip_if_no("xarray", min_version="0.7.0") + def test_to_xarray(self): + from xarray import DataArray + + s = Series([], dtype=object) + s.index.name = "foo" + result = s.to_xarray() + assert len(result) == 0 + assert len(result.coords) == 1 + tm.assert_almost_equal(list(result.coords.keys()), ["foo"]) + assert isinstance(result, DataArray) + + s = Series(range(6)) + s.index.name = "foo" + s.index = pd.MultiIndex.from_product( + [["a", "b"], range(3)], names=["one", "two"] + ) + result = s.to_xarray() + assert len(result) == 2 + tm.assert_almost_equal(list(result.coords.keys()), ["one", "two"]) + assert isinstance(result, DataArray) + tm.assert_series_equal(result.to_series(), s)