Esempio n. 1
0
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)
Esempio n. 2
0
def test_blank_coord(nancoord):
    """Empy strings and NaNs in the coords must trigger a ValueError as
    there is no way to round-trip them back
    """
    # Simple index
    i0 = xarray.DataArray(
        [10, 20], dims=['x'], coords={'x': nancoord}, name='i0')
    # MultiIndex on the columns; 1st level
    i1 = xarray.DataArray(
        [[10, 20], [30, 40]], dims=['x', 'y'],
        coords={'x': nancoord}, name='i1').stack(s=['x', 'y'])
    # MultiIndex on the rows; 1st level
    i2 = xarray.DataArray(
        [[10, 20], [30, 40]], dims=['x', 'y'],
        coords={'x': nancoord}, name='i2').stack(s=['x', 'y']).T
    # MultiIndex on the columns; 2nd level
    i3 = xarray.DataArray(
        [[10, 20], [30, 40]], dims=['x', 'y'],
        coords={'y': nancoord}, name='i3').stack(s=['x', 'y'])
    # MultiIndex on the rows; 2st level
    i4 = xarray.DataArray(
        [[10, 20], [30, 40]], dims=['x', 'y'],
        coords={'y': nancoord}, name='i4').stack(s=['x', 'y']).T

    if '' in nancoord:
        msg = 'Empty string in index'
    else:
        msg = 'NaN in index'

    for inp in (i0, i1, i2, i3, i4):
        print(inp)
        buf = io.StringIO()
        with pytest.raises(ValueError) as e:
            write_csv(inp, buf)
        assert str(e.value) == msg
Esempio n. 3
0
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)
Esempio n. 4
0
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")
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
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)
Esempio n. 8
0
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)
Esempio n. 9
0
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)
Esempio n. 10
0
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)
Esempio n. 11
0
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)
Esempio n. 12
0
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)
Esempio n. 13
0
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)
Esempio n. 14
0
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)
Esempio n. 15
0
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)
Esempio n. 16
0
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)
Esempio n. 17
0
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)
Esempio n. 18
0
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)
Esempio n. 19
0
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)
Esempio n. 20
0
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)
Esempio n. 21
0
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)
Esempio n. 22
0
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)
Esempio n. 23
0
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)
Esempio n. 24
0
def test_blank_coord(nancoord):
    """Empy strings and NaNs in the coords must trigger a ValueError as
    there is no way to round-trip them back
    """
    # Simple index
    i0 = xarray.DataArray([10, 20], dims=["x"], coords={"x": nancoord}, name="i0")
    # MultiIndex on the columns; 1st level
    i1 = xarray.DataArray(
        [[10, 20], [30, 40]], dims=["x", "y"], coords={"x": nancoord}, name="i1"
    ).stack(s=["x", "y"])
    # MultiIndex on the rows; 1st level
    i2 = (
        xarray.DataArray(
            [[10, 20], [30, 40]], dims=["x", "y"], coords={"x": nancoord}, name="i2"
        )
        .stack(s=["x", "y"])
        .T
    )
    # MultiIndex on the columns; 2nd level
    i3 = xarray.DataArray(
        [[10, 20], [30, 40]], dims=["x", "y"], coords={"y": nancoord}, name="i3"
    ).stack(s=["x", "y"])
    # MultiIndex on the rows; 2st level
    i4 = (
        xarray.DataArray(
            [[10, 20], [30, 40]], dims=["x", "y"], coords={"y": nancoord}, name="i4"
        )
        .stack(s=["x", "y"])
        .T
    )

    if "" in nancoord:
        msg = "Empty string in index"
    else:
        msg = "NaN in index"

    for inp in (i0, i1, i2, i3, i4):
        print(inp)
        buf = io.StringIO()
        with pytest.raises(ValueError) as e:
            write_csv(inp, buf)
        assert str(e.value) == msg
Esempio n. 25
0
def test_duplicate_index_multiindex(unstack):
    """Duplicate indices are OK"""
    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,\n10,10,1\n10,10,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.unstack() if unstack else a, b)
Esempio n. 26
0
def test_write_dataframe():
    a = pandas.DataFrame([[1, 2, 3], [4, 5, 6]])
    txt = ("dim_1,0,1,2\n"
           "dim_0,,,\n"
           "0,1,2,3\n"
           "1,4,5,6\n")
    buf = io.StringIO()
    write_csv(a, buf)
    print(buf.getvalue())
    assert buf.getvalue().replace('\r', '') == txt

    # add index/columns labels
    a.index = ['i1', 'i2']
    a.columns = ['c1', 'c2', 'c3']
    txt = ("dim_1,c1,c2,c3\n"
           "dim_0,,,\n"
           "i1,1,2,3\n"
           "i2,4,5,6\n")
    buf = io.StringIO()
    write_csv(a, buf)
    assert buf.getvalue().replace('\r', '') == txt

    # add index/columns names
    a.index.name = 'r'
    a.columns.name = 'c'
    txt = ("c,c1,c2,c3\n"
           "r,,,\n"
           "i1,1,2,3\n"
           "i2,4,5,6\n")
    buf = io.StringIO()
    write_csv(a, buf)
    print(buf.getvalue())
    assert buf.getvalue().replace('\r', '') == txt
Esempio n. 27
0
def test_write_series():
    a = pandas.Series([10, 20])
    txt = ("dim_0,\n"
           "0,10\n"
           "1,20\n")
    buf = io.StringIO()
    write_csv(a, buf)
    print(buf.getvalue())
    assert buf.getvalue().replace('\r', '') == txt

    # add index labels
    a.index = ['i1', 'i2']
    txt = ("dim_0,\n"
           "i1,10\n"
           "i2,20\n")
    buf = io.StringIO()
    write_csv(a, buf)
    print(buf.getvalue())
    assert buf.getvalue().replace('\r', '') == txt

    # add index/columns names
    a.index.name = 'r'
    txt = ("r,\n"
           "i1,10\n"
           "i2,20\n")
    buf = io.StringIO()
    write_csv(a, buf)
    print(buf.getvalue())
    assert buf.getvalue().replace('\r', '') == txt
Esempio n. 28
0
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)
Esempio n. 29
0
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)
Esempio n. 30
0
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)