From 6a9c801243ab5634565c98d417918a27226c717e Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 25 Feb 2020 16:40:51 -0800 Subject: [PATCH 1/2] implement test_to_xarray --- pandas/tests/generic/test_frame.py | 98 ---------------- pandas/tests/generic/test_series.py | 59 ---------- pandas/tests/generic/test_to_xarray.py | 154 +++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 157 deletions(-) create mode 100644 pandas/tests/generic/test_to_xarray.py diff --git a/pandas/tests/generic/test_frame.py b/pandas/tests/generic/test_frame.py index dca65152e82db..ab9401bea0c7b 100644 --- a/pandas/tests/generic/test_frame.py +++ b/pandas/tests/generic/test_frame.py @@ -1,25 +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 - 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 @@ -233,91 +223,3 @@ def test_unexpected_keyword(self): with pytest.raises(TypeError, match="unexpected keyword"): 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", - ) - @pytest.mark.parametrize("index", tm.all_index_generator(3)) - def test_to_xarray_index_types(self, 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 = index - 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 - # categoricals are not preserved - # 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, - check_index_type=False, - check_categorical=False, - ) - - @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 5aafd83da78fd..000ea8e0f414a 100644 --- a/pandas/tests/generic/test_series.py +++ b/pandas/tests/generic/test_series.py @@ -1,24 +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 .test_generic import Generic -try: - import xarray - - _XARRAY_INSTALLED = True -except ImportError: - _XARRAY_INSTALLED = False - class TestSeries(Generic): _typ = Series @@ -221,52 +211,3 @@ def test_get_default(self): for other in others: assert s.get(other, "z") == "z" assert s.get(other, other) == other - - -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", - ) - @pytest.mark.parametrize("index", tm.all_index_generator(6)) - def test_to_xarray_index_types(self, index): - from xarray import DataArray - - s = Series(range(6), index=index) - s.index.name = "foo" - result = s.to_xarray() - repr(result) - assert len(result) == 6 - 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, check_categorical=True - ) - - @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) diff --git a/pandas/tests/generic/test_to_xarray.py b/pandas/tests/generic/test_to_xarray.py new file mode 100644 index 0000000000000..62aceb4a2224c --- /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", + ) + @pytest.mark.parametrize("index", tm.all_index_generator(3)) + def test_to_xarray_index_types(self, 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 = index + 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 + # categoricals are not preserved + # 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, + check_index_type=False, + check_categorical=False, + ) + + @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", + ) + @pytest.mark.parametrize("index", tm.all_index_generator(6)) + def test_to_xarray_index_types(self, index): + from xarray import DataArray + + s = Series(range(6), index=index) + s.index.name = "foo" + result = s.to_xarray() + repr(result) + assert len(result) == 6 + 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, check_categorical=True + ) + + @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) From 662f42ce85247970c8947895b3257f9a9034722d Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 22 Mar 2020 12:05:10 -0700 Subject: [PATCH 2/2] dummy commit to force CI