From 627758d677dce06d847bdd96312d791949a8511d Mon Sep 17 00:00:00 2001 From: Joseph Hamman Date: Fri, 1 Feb 2019 22:53:51 -0800 Subject: [PATCH 1/2] add tests for GH#697 - handling of empty pandas objects in constructors --- xarray/tests/test_dataarray.py | 9 +++++++++ xarray/tests/test_dataset.py | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 9b4f8523178..abeafc43df2 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -2824,6 +2824,15 @@ def test_to_and_from_series(self): assert_identical( expected_da, DataArray.from_series(actual).drop(['x', 'y'])) + + def test_to_and_from_empty_series(self): + # GH697 + expected = pd.Series([]) + da = DataArray.from_series(expected) + assert len(da) == 0 + actual = da.to_series() + assert len(actual) == 0 + assert expected.equals(actual) def test_series_categorical_index(self): # regression test for GH700 diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 6f6287efcac..1dc541a8e2a 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2984,6 +2984,15 @@ def test_to_and_from_dataframe(self): expected = pd.DataFrame([[]], index=idx) assert expected.equals(actual), (expected, actual) + def test_to_and_from_empty_dataframe(self): + # GH697 + expected = pd.DataFrame({'foo' : []}) + ds = Dataset.from_dataframe(expected) + assert len(ds['foo']) == 0 + actual = ds.to_dataframe() + assert len(actual) == 0 + assert expected.equals(actual) + def test_from_dataframe_non_unique_columns(self): # regression test for GH449 df = pd.DataFrame(np.zeros((2, 2))) From 731616dd6acc802462d59f2224f0549fa1bb3c18 Mon Sep 17 00:00:00 2001 From: Joseph Hamman Date: Fri, 1 Feb 2019 22:56:00 -0800 Subject: [PATCH 2/2] make pep8 happy --- xarray/tests/test_dataarray.py | 2 +- xarray/tests/test_dataset.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index abeafc43df2..76684a2f5c9 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -2824,7 +2824,7 @@ def test_to_and_from_series(self): assert_identical( expected_da, DataArray.from_series(actual).drop(['x', 'y'])) - + def test_to_and_from_empty_series(self): # GH697 expected = pd.Series([]) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 1dc541a8e2a..5acf96a87e7 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2986,7 +2986,7 @@ def test_to_and_from_dataframe(self): def test_to_and_from_empty_dataframe(self): # GH697 - expected = pd.DataFrame({'foo' : []}) + expected = pd.DataFrame({'foo': []}) ds = Dataset.from_dataframe(expected) assert len(ds['foo']) == 0 actual = ds.to_dataframe()