|
8 | 8 |
|
9 | 9 | from distutils.version import LooseVersion
|
10 | 10 | from pandas import (Index, Series, DataFrame, Panel, isnull,
|
11 |
| - date_range, period_range) |
| 11 | + date_range, period_range, Panel4D) |
12 | 12 | from pandas.core.index import MultiIndex
|
13 | 13 |
|
14 | 14 | import pandas.core.common as com
|
|
18 | 18 | from pandas.util.testing import (assert_series_equal,
|
19 | 19 | assert_frame_equal,
|
20 | 20 | assert_panel_equal,
|
| 21 | + assert_panel4d_equal, |
| 22 | + assert_almost_equal, |
21 | 23 | assert_equal)
|
22 | 24 | import pandas.util.testing as tm
|
23 | 25 |
|
@@ -1057,6 +1059,52 @@ def test_describe_none(self):
|
1057 | 1059 | expected = Series([0, 0], index=['count', 'unique'], name='None')
|
1058 | 1060 | assert_series_equal(noneSeries.describe(), expected)
|
1059 | 1061 |
|
| 1062 | + def test_to_xarray(self): |
| 1063 | + |
| 1064 | + tm._skip_if_no_xarray() |
| 1065 | + from xarray import DataArray |
| 1066 | + |
| 1067 | + s = Series([]) |
| 1068 | + s.index.name = 'foo' |
| 1069 | + result = s.to_xarray() |
| 1070 | + self.assertEqual(len(result), 0) |
| 1071 | + self.assertEqual(len(result.coords), 1) |
| 1072 | + assert_almost_equal(list(result.coords.keys()), ['foo']) |
| 1073 | + self.assertIsInstance(result, DataArray) |
| 1074 | + |
| 1075 | + def testit(index, check_index_type=True): |
| 1076 | + s = Series(range(6), index=index(6)) |
| 1077 | + s.index.name = 'foo' |
| 1078 | + result = s.to_xarray() |
| 1079 | + repr(result) |
| 1080 | + self.assertEqual(len(result), 6) |
| 1081 | + self.assertEqual(len(result.coords), 1) |
| 1082 | + assert_almost_equal(list(result.coords.keys()), ['foo']) |
| 1083 | + self.assertIsInstance(result, DataArray) |
| 1084 | + |
| 1085 | + # idempotency |
| 1086 | + assert_series_equal(result.to_series(), s, |
| 1087 | + check_index_type=check_index_type) |
| 1088 | + |
| 1089 | + for index in [tm.makeFloatIndex, tm.makeIntIndex, |
| 1090 | + tm.makeStringIndex, tm.makeUnicodeIndex, |
| 1091 | + tm.makeDateIndex, tm.makePeriodIndex, |
| 1092 | + tm.makeTimedeltaIndex]: |
| 1093 | + testit(index) |
| 1094 | + |
| 1095 | + # not idempotent |
| 1096 | + testit(tm.makeCategoricalIndex, check_index_type=False) |
| 1097 | + |
| 1098 | + s = Series(range(6)) |
| 1099 | + s.index.name = 'foo' |
| 1100 | + s.index = pd.MultiIndex.from_product([['a', 'b'], range(3)], |
| 1101 | + names=['one', 'two']) |
| 1102 | + result = s.to_xarray() |
| 1103 | + self.assertEqual(len(result), 2) |
| 1104 | + assert_almost_equal(list(result.coords.keys()), ['one', 'two']) |
| 1105 | + self.assertIsInstance(result, DataArray) |
| 1106 | + assert_series_equal(result.to_series(), s) |
| 1107 | + |
1060 | 1108 |
|
1061 | 1109 | class TestDataFrame(tm.TestCase, Generic):
|
1062 | 1110 | _typ = DataFrame
|
@@ -1777,11 +1825,103 @@ def test_pct_change(self):
|
1777 | 1825 |
|
1778 | 1826 | self.assert_frame_equal(result, expected)
|
1779 | 1827 |
|
| 1828 | + def test_to_xarray(self): |
| 1829 | + |
| 1830 | + tm._skip_if_no_xarray() |
| 1831 | + from xarray import Dataset |
| 1832 | + |
| 1833 | + df = DataFrame({'a': list('abc'), |
| 1834 | + 'b': list(range(1, 4)), |
| 1835 | + 'c': np.arange(3, 6).astype('u1'), |
| 1836 | + 'd': np.arange(4.0, 7.0, dtype='float64'), |
| 1837 | + 'e': [True, False, True], |
| 1838 | + 'f': pd.Categorical(list('abc')), |
| 1839 | + 'g': pd.date_range('20130101', periods=3), |
| 1840 | + 'h': pd.date_range('20130101', |
| 1841 | + periods=3, |
| 1842 | + tz='US/Eastern')} |
| 1843 | + ) |
| 1844 | + |
| 1845 | + df.index.name = 'foo' |
| 1846 | + result = df[0:0].to_xarray() |
| 1847 | + self.assertEqual(result.dims['foo'], 0) |
| 1848 | + self.assertIsInstance(result, Dataset) |
| 1849 | + |
| 1850 | + for index in [tm.makeFloatIndex, tm.makeIntIndex, |
| 1851 | + tm.makeStringIndex, tm.makeUnicodeIndex, |
| 1852 | + tm.makeDateIndex, tm.makePeriodIndex, |
| 1853 | + tm.makeCategoricalIndex, tm.makeTimedeltaIndex]: |
| 1854 | + df.index = index(3) |
| 1855 | + df.index.name = 'foo' |
| 1856 | + df.columns.name = 'bar' |
| 1857 | + result = df.to_xarray() |
| 1858 | + self.assertEqual(result.dims['foo'], 3) |
| 1859 | + self.assertEqual(len(result.coords), 1) |
| 1860 | + self.assertEqual(len(result.data_vars), 8) |
| 1861 | + assert_almost_equal(list(result.coords.keys()), ['foo']) |
| 1862 | + self.assertIsInstance(result, Dataset) |
| 1863 | + |
| 1864 | + # idempotency |
| 1865 | + # categoricals are not preserved |
| 1866 | + # datetimes w/tz are not preserved |
| 1867 | + # column names are lost |
| 1868 | + expected = df.copy() |
| 1869 | + expected['f'] = expected['f'].astype(object) |
| 1870 | + expected['h'] = expected['h'].astype('datetime64[ns]') |
| 1871 | + expected.columns.name = None |
| 1872 | + assert_frame_equal(result.to_dataframe(), |
| 1873 | + expected, |
| 1874 | + check_index_type=False) |
| 1875 | + |
| 1876 | + # not implemented |
| 1877 | + df.index = pd.MultiIndex.from_product([['a'], range(3)], |
| 1878 | + names=['one', 'two']) |
| 1879 | + self.assertRaises(ValueError, lambda: df.to_xarray()) |
| 1880 | + |
1780 | 1881 |
|
1781 | 1882 | class TestPanel(tm.TestCase, Generic):
|
1782 | 1883 | _typ = Panel
|
1783 | 1884 | _comparator = lambda self, x, y: assert_panel_equal(x, y)
|
1784 | 1885 |
|
| 1886 | + def test_to_xarray(self): |
| 1887 | + |
| 1888 | + tm._skip_if_no_xarray() |
| 1889 | + from xarray import DataArray |
| 1890 | + |
| 1891 | + p = tm.makePanel() |
| 1892 | + |
| 1893 | + result = p.to_xarray() |
| 1894 | + self.assertIsInstance(result, DataArray) |
| 1895 | + self.assertEqual(len(result.coords), 3) |
| 1896 | + assert_almost_equal(list(result.coords.keys()), |
| 1897 | + ['items', 'major_axis', 'minor_axis']) |
| 1898 | + self.assertEqual(len(result.dims), 3) |
| 1899 | + |
| 1900 | + # idempotency |
| 1901 | + assert_panel_equal(result.to_pandas(), p) |
| 1902 | + |
| 1903 | + |
| 1904 | +class TestPanel4D(tm.TestCase, Generic): |
| 1905 | + _typ = Panel4D |
| 1906 | + _comparator = lambda self, x, y: assert_panel4d_equal(x, y) |
| 1907 | + |
| 1908 | + def test_to_xarray(self): |
| 1909 | + |
| 1910 | + tm._skip_if_no_xarray() |
| 1911 | + from xarray import DataArray |
| 1912 | + |
| 1913 | + p = tm.makePanel4D() |
| 1914 | + |
| 1915 | + result = p.to_xarray() |
| 1916 | + self.assertIsInstance(result, DataArray) |
| 1917 | + self.assertEqual(len(result.coords), 4) |
| 1918 | + assert_almost_equal(list(result.coords.keys()), |
| 1919 | + ['labels', 'items', 'major_axis', 'minor_axis']) |
| 1920 | + self.assertEqual(len(result.dims), 4) |
| 1921 | + |
| 1922 | + # non-convertible |
| 1923 | + self.assertRaises(ValueError, lambda: result.to_pandas()) |
| 1924 | + |
1785 | 1925 |
|
1786 | 1926 | class TestNDFrame(tm.TestCase):
|
1787 | 1927 | # tests that don't fit elsewhere
|
|
0 commit comments