def test_zip():
    x = awkward1.Array([1.1, 2.2, 3.3])
    y = awkward1.Array(["one", "two", "three"])
    assert awkward1.zip({
        "x": x,
        "y": y
    }).tolist() == [{
        "x": 1.1,
        "y": "one"
    }, {
        "x": 2.2,
        "y": "two"
    }, {
        "x": 3.3,
        "y": "three"
    }]
    y = awkward1.to_categorical(y)
    assert awkward1.zip({
        "x": x,
        "y": y
    }).tolist() == [{
        "x": 1.1,
        "y": "one"
    }, {
        "x": 2.2,
        "y": "two"
    }, {
        "x": 3.3,
        "y": "three"
    }]
Exemple #2
0
def test():
    awkward1.deprecations_as_errors = True

    array = awkward1.to_categorical(awkward1.Array([321, 1.1, 123, 1.1, 999, 1.1, 2.0]))
    assert awkward1.to_list(array * 10) == [3210, 11, 1230, 11, 9990, 11, 20]

    array = awkward1.Array(["HAL"])
    with pytest.raises(ValueError):
        array + 1
def test_to_categorical_nested():
    array = awkward1.Array([["one", "two", "three"], [], ["one", "two"],
                            ["three"]])
    assert not awkward1.is_categorical(array)
    categorical = awkward1.to_categorical(array)
    assert awkward1.is_categorical(categorical)
    assert awkward1.to_list(array) == categorical.tolist()
    not_categorical = awkward1.from_categorical(categorical)
    assert not awkward1.is_categorical(not_categorical)
    assert awkward1.categories(categorical).tolist() == ["one", "two", "three"]
def test_to_categorical_numbers():
    array = awkward1.Array([1.1, 2.2, 3.3, 1.1, 2.2, 3.3, 1.1, 2.2, 3.3])
    assert not awkward1.is_categorical(array)
    categorical = awkward1.to_categorical(array)
    assert awkward1.is_categorical(categorical)
    assert awkward1.to_list(array) == categorical.tolist()
    assert awkward1.to_list(categorical.layout.content) == [1.1, 2.2, 3.3]
    not_categorical = awkward1.from_categorical(categorical)
    assert not awkward1.is_categorical(not_categorical)
    assert awkward1.categories(categorical).tolist() == [1.1, 2.2, 3.3]
def test_to_categorical():
    array = awkward1.Array(
        ["one", "two", "three", "one", "two", "three", "one", "two", "three"])
    assert not awkward1.is_categorical(array)
    categorical = awkward1.to_categorical(array)
    assert awkward1.is_categorical(categorical)
    assert awkward1.to_list(array) == categorical.tolist()
    assert awkward1.to_list(
        categorical.layout.content) == ["one", "two", "three"]
    not_categorical = awkward1.from_categorical(categorical)
    assert not awkward1.is_categorical(not_categorical)
    assert awkward1.categories(categorical).tolist() == ["one", "two", "three"]
def test_typestr():
    if not awkward1._util.py27:
        assert str(
            awkward1.type(
                awkward1.to_categorical(awkward1.Array(
                    [1.1, 2.2, 2.2, 3.3])))) == "4 * categorical[type=float64]"
        assert str(
            awkward1.type(
                awkward1.to_categorical(
                    awkward1.Array([1.1, 2.2, None, 2.2, 3.3
                                    ])))) == "5 * categorical[type=?float64]"
        assert str(
            awkward1.type(
                awkward1.to_categorical(
                    awkward1.Array(["one", "two", "two", "three"
                                    ])))) == "4 * categorical[type=string]"
        assert str(
            awkward1.type(
                awkward1.to_categorical(
                    awkward1.Array(
                        ["one", "two", None, "two",
                         "three"])))) == "5 * categorical[type=option[string]]"
def test_to_categorical_masked():
    content = awkward1.Array([
        "one", "two", "three", "one", "one", "two", "three", "two", "one",
        "two", "three", "three"
    ]).layout
    mask = awkward1.layout.Index8(
        numpy.array([
            False, False, False, True, False, False, False, True, False, False,
            False, True
        ]))
    array = awkward1.Array(
        awkward1.layout.ByteMaskedArray(mask, content, valid_when=False))
    assert not awkward1.is_categorical(array)
    categorical = awkward1.to_categorical(array)
    assert awkward1.is_categorical(categorical)
    assert awkward1.to_list(array) == categorical.tolist()
    assert awkward1.to_list(
        categorical.layout.content) == ["one", "two", "three"]
    not_categorical = awkward1.from_categorical(categorical)
    assert not awkward1.is_categorical(not_categorical)
    assert awkward1.categories(categorical).tolist() == ["one", "two", "three"]
def test_to_categorical_masked():
    content = awkward1.Array(
        ["one", "two", "three", "one", "one", "two", "three", "two"]).layout
    index = awkward1.layout.Index64(
        numpy.array([0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3], dtype=numpy.int64))
    indexedarray = awkward1.layout.IndexedArray64(index, content)
    mask = awkward1.layout.Index8(
        numpy.array([
            False, False, False, True, False, False, False, True, False, False,
            False, True
        ]))
    array = awkward1.Array(
        awkward1.layout.ByteMaskedArray(mask, indexedarray, valid_when=False))
    assert not awkward1.is_categorical(array)
    categorical = awkward1.to_categorical(array)
    assert awkward1.is_categorical(categorical)
    assert awkward1.to_list(array) == categorical.tolist()
    assert awkward1.to_list(
        categorical.layout.content) == ["one", "two", "three"]
    not_categorical = awkward1.from_categorical(categorical)
    assert not awkward1.is_categorical(not_categorical)
    assert awkward1.categories(categorical).tolist() == ["one", "two", "three"]