def test_2d_multiindex_rows(): a = xarray.DataArray( np.arange(2 * 3 * 4).reshape((2, 3, 4)), dims=['x', 'y', 'z'], coords={ 'x': ['x0', 'x1'], 'y': ['y2', 'y0', 'y1'], # test order is not compromised 'z': ['z0', 'z2', 'z1', 'z3'] }) b = a.stack(dim_0=['y', 'z']).T txt = ("x,,x0,x1\n" "y,z,,\n" "y2,z0,0,12\n" "y2,z2,1,13\n" "y2,z1,2,14\n" "y2,z3,3,15\n" "y0,z0,4,16\n" "y0,z2,5,17\n" "y0,z1,6,18\n" "y0,z3,7,19\n" "y1,z0,8,20\n" "y1,z2,9,21\n" "y1,z1,10,22\n" "y1,z3,11,23\n") buf = io.StringIO() write_csv(b, buf) assert buf.getvalue().replace('\r', '') == txt buf.seek(0) c = read_csv(buf) xarray.testing.assert_equal(a, c) buf.seek(0) c = read_csv(buf, unstack=False) xarray.testing.assert_equal(b, c)
def test_2d_multiindex_both(explicit_stack): a = xarray.DataArray(np.arange(16).reshape((2, 2, 2, 2)), dims=['x', 'y', 'z', 'w'], coords={ 'x': ['x0', 'x1'], 'y': ['y0', 'y1'], 'z': ['z0', 'z1'], 'w': ['w0', 'w1'] }) b = a.stack(dim_0=['x', 'y']).transpose('dim_0', 'z', 'w') c = b.stack(dim_1=['z', 'w']) txt = ("z,,z0,z0,z1,z1\n" "w,,w0,w1,w0,w1\n" "x,y,,,,\n" "x0,y0,0,1,2,3\n" "x0,y1,4,5,6,7\n" "x1,y0,8,9,10,11\n" "x1,y1,12,13,14,15\n") buf = io.StringIO() if explicit_stack: write_csv(c, buf) else: write_csv(b, buf) assert buf.getvalue().replace('\r', '') == txt buf.seek(0) d = read_csv(buf) xarray.testing.assert_equal(a, d) buf.seek(0) d = read_csv(buf, unstack=False) xarray.testing.assert_equal(c, d)
def test_duplicate_index_multiindex(): """Duplicate indices are OK as long as you don't try unstacking """ a = xarray.DataArray( [1, 2], dims=['dim_0'], coords={ 'dim_0': pandas.MultiIndex.from_tuples([(10, 10), (10, 10)], names=['x', 'y']) }) txt = ("x,y,\n" "10,10,1\n" "10,10,2\n") buf = io.StringIO() write_csv(a, buf) assert buf.getvalue().replace('\r', '') == txt buf.seek(0) b = read_csv(buf, unstack=False) xarray.testing.assert_equal(a, b) buf.seek(0) with pytest.raises(ValueError) as e: read_csv(buf, unstack=True) assert str(e.value) == ("cannot reindex or align along dimension 'dim_0' " "because the index has duplicate values")
def test_2d_multiindex_cols(explicit_stack): a = xarray.DataArray( np.arange(2 * 3 * 4).reshape((2, 3, 4)), dims=['x', 'y', 'z'], coords={ 'x': ['x0', 'x1'], 'y': ['y2', 'y0', 'y1'], # test order is not compromised 'z': ['z0', 'z2', 'z1', 'z3'] }) b = a.stack(dim_1=['y', 'z']) txt = ("y,y2,y2,y2,y2,y0,y0,y0,y0,y1,y1,y1,y1\n" "z,z0,z2,z1,z3,z0,z2,z1,z3,z0,z2,z1,z3\n" "x,,,,,,,,,,,,\n" "x0,0,1,2,3,4,5,6,7,8,9,10,11\n" "x1,12,13,14,15,16,17,18,19,20,21,22,23\n") buf = io.StringIO() # >2 dimensional arrays get their dimensions beyond the first automatically # stacked if explicit_stack: write_csv(b, buf) else: write_csv(a, buf) assert buf.getvalue().replace('\r', '') == txt buf.seek(0) c = read_csv(buf) xarray.testing.assert_equal(a, c) buf.seek(0) c = read_csv(buf, unstack=False) xarray.testing.assert_equal(b, c)
def test_ambiguous_nonindex_coords(): """Test when non-index coords have multiple values for the matching index coord """ buf = io.StringIO("x,y,z (x),\n" "0,0,0,1\n" "0,1,1,1\n") with pytest.raises(ValueError) as e: read_csv(buf) assert str(e.value) == ("Non-index coord z (x) has different values for " "the same value of its dimension x")
def test_coords_bool(): buf = io.StringIO("y,true,false,TRUE,FALSE,True,False,Y,N,y,n,YES,NO," "Yes,No,yes,no,T,F,T,F\n" "x\n" "x0" + ",1" * 20 + "\n") a = read_csv(buf) assert a.y.values.tolist() == [True, False] * 10
def test_nonindex_coords_with_multiindex(unstack): buf = io.StringIO( "x,y,z (x),\nx1,y1,z1,1\nx1,y2,z1,2\nx2,y1,z2,3\nx2,y2,z2,4\n") a = read_csv(buf, unstack=unstack) if unstack: b = xarray.DataArray( [[1, 2], [3, 4]], dims=["x", "y"], coords={ "x": ["x1", "x2"], "y": ["y1", "y2"], "z": ("x", ["z1", "z2"]), }, ) else: b = xarray.DataArray( [1, 2, 3, 4], dims=["dim_0"], coords={ "dim_0": pandas.MultiIndex.from_tuples( [("x1", "y1"), ("x1", "y2"), ("x2", "y1"), ("x2", "y2")], names=["x", "y"], ), "z": ("dim_0", ["z1", "z1", "z2", "z2"]), }, ) xarray.testing.assert_equal(a, b)
def test_1d_multiindex(): a = xarray.DataArray( [[1, 2], [3, 4]], dims=["r", "c"], coords={"r": [10, 20], "c": [30, 40]} ) b = a.stack(dim_0=["r", "c"]) txt = "r,c,\n10,30,1\n10,40,2\n20,30,3\n20,40,4\n" buf = io.StringIO() write_csv(b, buf) assert buf.getvalue().replace("\r", "") == txt buf.seek(0) c = read_csv(buf) xarray.testing.assert_equal(c, a) buf.seek(0) c = read_csv(buf, unstack=False) xarray.testing.assert_equal(c, b)
def test_nonindex_coords_with_multiindex(unstack): buf = io.StringIO("x,y,z (x),\n" "x1,y1,z1,1\n" "x1,y2,z1,2\n" "x2,y1,z2,3\n" "x2,y2,z2,4\n") a = read_csv(buf, unstack=unstack) if unstack: b = xarray.DataArray([[1, 2], [3, 4]], dims=['x', 'y'], coords={ 'x': ['x1', 'x2'], 'y': ['y1', 'y2'], 'z': ('x', ['z1', 'z2']), }) else: b = xarray.DataArray( [1, 2, 3, 4], dims=['dim_0'], coords={ 'dim_0': pandas.MultiIndex.from_tuples([('x1', 'y1'), ('x1', 'y2'), ('x2', 'y1'), ('x2', 'y2')], names=['x', 'y']), 'z': ('dim_0', ['z1', 'z1', 'z2', 'z2']), }) xarray.testing.assert_equal(a, b)
def test_file_io(tmpdir, open_func, ext): a = xarray.DataArray(1) fname = f"{tmpdir}/test.{ext}" write_csv(a, fname) with open_func(fname, "rt") as fh: assert fh.read() == "1\n" b = read_csv(fname) xarray.testing.assert_equal(a, b)
def test_numerical_ids2(): a = xarray.DataArray([1, 2], dims=['x'], coords={'x': ['01', '02']}) b = xarray.DataArray([1, 2], dims=['x'], coords={'x': [1, 2]}) buf = io.StringIO() write_csv(a, buf) buf.seek(0) c = read_csv(buf) xarray.testing.assert_equal(b, c)
def test_buf_io(): a = xarray.DataArray(1) buf = io.StringIO() write_csv(a, buf) assert buf.getvalue() == '1\n' buf.seek(0) b = read_csv(buf) xarray.testing.assert_equal(a, b)
def test_file_io(tmpdir, open_func, ext): a = xarray.DataArray(1) fname = '%s/test.%s' % (tmpdir, ext) write_csv(a, fname) with open_func(fname, 'rt') as fh: assert fh.read() == '1\n' b = read_csv(fname) xarray.testing.assert_equal(a, b)
def test_numerical_ids2(): a = xarray.DataArray([1, 2], dims=["x"], coords={"x": ["01", "02"]}) b = xarray.DataArray([1, 2], dims=["x"], coords={"x": [1, 2]}) buf = io.StringIO() write_csv(a, buf) buf.seek(0) c = read_csv(buf) xarray.testing.assert_equal(b, c)
def test_missing_index_coord1(unstack): """The index coord may be missing as long as matching non-index coord(s) spell out the dim name """ buf = io.StringIO("y (x),\n" "10,1\n" "20,2\n") b = xarray.DataArray([1, 2], dims=['x'], coords={'y': ('x', [10, 20])}) a = read_csv(buf, unstack=unstack) xarray.testing.assert_equal(a, b)
def test_1d(data, txt): a = xarray.DataArray(data, dims=["x"], coords={"x": ["x1", "x2"]}) buf = io.StringIO() write_csv(a, buf) assert buf.getvalue().replace("\r", "") == txt buf.seek(0) b = read_csv(buf) xarray.testing.assert_equal(a, b)
def test_1d(data, txt): a = xarray.DataArray(data, dims=['x'], coords={'x': ['x1', 'x2']}) buf = io.StringIO() write_csv(a, buf) assert buf.getvalue().replace('\r', '') == txt buf.seek(0) b = read_csv(buf) xarray.testing.assert_equal(a, b)
def test_0d(data, txt): a = xarray.DataArray(data) buf = io.StringIO() write_csv(a, buf) assert buf.getvalue().replace('\r', '') == txt buf.seek(0) b = read_csv(buf) xarray.testing.assert_equal(a, b)
def test_shape1(): # Test the edge case of an array with shape (1, ) a = xarray.DataArray([1], dims=['x'], coords={'x': ['x1']}) buf = io.StringIO() write_csv(a, buf) assert buf.getvalue().replace('\r', '') == 'x,\nx1,1\n' buf.seek(0) b = read_csv(buf) xarray.testing.assert_equal(a, b)
def test_shape1(): # Test the edge case of an array with shape (1, ) a = xarray.DataArray([1], dims=["x"], coords={"x": ["x1"]}) buf = io.StringIO() write_csv(a, buf) assert buf.getvalue().replace("\r", "") == "x,\nx1,1\n" buf.seek(0) b = read_csv(buf) xarray.testing.assert_equal(a, b)
def test_1d_multiindex(): a = xarray.DataArray([[1, 2], [3, 4]], dims=['r', 'c'], coords={ 'r': [10, 20], 'c': [30, 40] }) b = a.stack(dim_0=['r', 'c']) txt = ("r,c,\n" "10,30,1\n" "10,40,2\n" "20,30,3\n" "20,40,4\n") buf = io.StringIO() write_csv(b, buf) assert buf.getvalue().replace('\r', '') == txt buf.seek(0) c = read_csv(buf) xarray.testing.assert_equal(c, a) buf.seek(0) c = read_csv(buf, unstack=False) xarray.testing.assert_equal(c, b)
def test_nonindex_coords(unstack): a = xarray.DataArray( [1, 2], dims=["x"], coords={"x": [10, 20], "y": ("x", [30, 40])} ) txt = "x,y (x),\n10,30,1\n20,40,2\n" buf = io.StringIO() write_csv(a, buf) assert buf.getvalue().replace("\r", "") == txt buf.seek(0) b = read_csv(buf, unstack=unstack) xarray.testing.assert_equal(a, b)
def test_numerical_ids1(): """An index full of numerical IDs padded on the left with zeros won't accidentally convert them to int as long as at least one element can't be cast to int """ a = xarray.DataArray([1, 2, 3], dims=["x"], coords={"x": ["01", "02", "S1"]}) buf = io.StringIO() write_csv(a, buf) buf.seek(0) b = read_csv(buf) xarray.testing.assert_equal(a, b)
def test_duplicate_index(): """Duplicate indices are OK as long as you don't try unstacking""" a = xarray.DataArray([1, 2], dims=["x"], coords={"x": [10, 10]}) txt = "x,\n10,1\n10,2\n" buf = io.StringIO() write_csv(a, buf) assert buf.getvalue().replace("\r", "") == txt buf.seek(0) b = read_csv(buf) xarray.testing.assert_equal(a, b)
def test_missing_index_coord2(unstack): """Same as above, but with 2 non-index coords which will instigate pandas to create a MultiIndex """ buf = io.StringIO("y (x),z (x),\n" "10,30,1\n" "20,40,2\n") b = xarray.DataArray([1, 2], dims=['x'], coords={ 'y': ('x', [10, 20]), 'z': ('x', [30, 40]) }) a = read_csv(buf, unstack=unstack) xarray.testing.assert_equal(a, b)
def test_xarray_nocoords(): a = xarray.DataArray([[1, 2], [3, 4]], dims=['r', 'c']) b = a.copy() b.coords['r'] = [0, 1] b.coords['c'] = [0, 1] txt = ("c,0,1\n" "r,,\n" "0,1,2\n" "1,3,4\n") buf = io.StringIO() write_csv(a, buf) assert buf.getvalue().replace('\r', '') == txt buf.seek(0) c = read_csv(buf) xarray.testing.assert_equal(b, c)
def test_xarray_nocoords(): a = xarray.DataArray([[1, 2], [3, 4]], dims=["r", "c"]) b = a.copy() b.coords["r"] = [0, 1] b.coords["c"] = [0, 1] txt = "c,0,1\nr,,\n0,1,2\n1,3,4\n" buf = io.StringIO() write_csv(a, buf) assert buf.getvalue().replace("\r", "") == txt buf.seek(0) c = read_csv(buf) xarray.testing.assert_equal(b, c)
def test_2d_multiindex_rows(): a = xarray.DataArray( np.arange(2 * 3 * 4).reshape((2, 3, 4)), dims=["x", "y", "z"], coords={ "x": ["x0", "x1"], "y": ["y2", "y0", "y1"], # test order is not compromised "z": ["z0", "z2", "z1", "z3"], }, ) b = a.stack(dim_0=["y", "z"]).T txt = ( "x,,x0,x1\n" "y,z,,\n" "y2,z0,0,12\n" "y2,z2,1,13\n" "y2,z1,2,14\n" "y2,z3,3,15\n" "y0,z0,4,16\n" "y0,z2,5,17\n" "y0,z1,6,18\n" "y0,z3,7,19\n" "y1,z0,8,20\n" "y1,z2,9,21\n" "y1,z1,10,22\n" "y1,z3,11,23\n" ) buf = io.StringIO() write_csv(b, buf) assert buf.getvalue().replace("\r", "") == txt buf.seek(0) c = read_csv(buf) xarray.testing.assert_equal(a, c) buf.seek(0) c = read_csv(buf, unstack=False) xarray.testing.assert_equal(b, c)
def test_nonindex_coords(unstack): a = xarray.DataArray([1, 2], dims=['x'], coords={ 'x': [10, 20], 'y': ('x', [30, 40]) }) txt = ("x,y (x),\n" "10,30,1\n" "20,40,2\n") buf = io.StringIO() write_csv(a, buf) assert buf.getvalue().replace('\r', '') == txt buf.seek(0) b = read_csv(buf, unstack=unstack) xarray.testing.assert_equal(a, b)
def test_2d_multiindex_both(explicit_stack): a = xarray.DataArray( np.arange(16).reshape((2, 2, 2, 2)), dims=["x", "y", "z", "w"], coords={ "x": ["x0", "x1"], "y": ["y0", "y1"], "z": ["z0", "z1"], "w": ["w0", "w1"], }, ) b = a.stack(dim_0=["x", "y"]).transpose("dim_0", "z", "w") c = b.stack(dim_1=["z", "w"]) txt = ( "z,,z0,z0,z1,z1\n" "w,,w0,w1,w0,w1\n" "x,y,,,,\n" "x0,y0,0,1,2,3\n" "x0,y1,4,5,6,7\n" "x1,y0,8,9,10,11\n" "x1,y1,12,13,14,15\n" ) buf = io.StringIO() if explicit_stack: write_csv(c, buf) else: write_csv(b, buf) assert buf.getvalue().replace("\r", "") == txt buf.seek(0) d = read_csv(buf) xarray.testing.assert_equal(a, d) buf.seek(0) d = read_csv(buf, unstack=False) xarray.testing.assert_equal(c, d)