def test_typestr(): assert (str(ak.type(ak.to_categorical(ak.Array( [1.1, 2.2, 2.2, 3.3])))) == "4 * categorical[type=float64]") assert (str( ak.type(ak.to_categorical(ak.Array( [1.1, 2.2, None, 2.2, 3.3])))) == "5 * categorical[type=?float64]") assert (str( ak.type(ak.to_categorical(ak.Array( ["one", "two", "two", "three"])))) == "4 * categorical[type=string]") assert (str( ak.type( ak.to_categorical(ak.Array( ["one", "two", None, "two", "three"])))) == "5 * categorical[type=option[string]]")
def test_arraytype_categorical_1(): text = str( ak.to_categorical( ak.Array(["one", "one", "two", "three", "one", "three"])).type) parsedtype = ak.types.from_datashape(text, True) assert isinstance(parsedtype, ak.types.ArrayType) assert str(parsedtype) == text
def test(): array = ak.to_categorical(ak.Array([321, 1.1, 123, 1.1, 999, 1.1, 2.0])) assert ak.to_list(array * 10) == [3210, 11, 1230, 11, 9990, 11, 20] array = ak.Array(["HAL"]) with pytest.raises(ValueError): array + 1
def test_jim4(): text = str( ak.to_categorical(ak.Array([1.1, 1.1, 2.2, 3.3, 1.1, 3.3])).type) print(text) parsedtype = deduce_type(text, True) assert isinstance(parsedtype, ak.types.ArrayType) assert str(parsedtype) == text
def test_to_categorical_masked(): content = ak.Array( ["one", "two", "three", "one", "one", "two", "three", "two"]).layout index = ak.layout.Index64( np.array([0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3], dtype=np.int64)) indexedarray = ak.layout.IndexedArray64(index, content) mask = ak.layout.Index8( np.array([ False, False, False, True, False, False, False, True, False, False, False, True, ])) array = ak.Array( ak.layout.ByteMaskedArray(mask, indexedarray, valid_when=False)) assert not ak.is_categorical(array) categorical = ak.to_categorical(array) assert ak.is_categorical(categorical) assert ak.to_list(array) == categorical.tolist() assert ak.to_list(categorical.layout.content) == ["one", "two", "three"] not_categorical = ak.from_categorical(categorical) assert not ak.is_categorical(not_categorical) assert ak.categories(categorical).tolist() == ["one", "two", "three"]
def test_jim3(): text = str( ak.to_categorical( ak.Array(["one", "one", "two", "three", "one", "three"])).type) print(text) parsedtype = deduce_type(text, True) assert isinstance(parsedtype, ak.types.ArrayType) assert str(parsedtype) == text
def test_to_categorical_nested(): array = ak.Array([["one", "two", "three"], [], ["one", "two"], ["three"]]) assert not ak.is_categorical(array) categorical = ak.to_categorical(array) assert ak.is_categorical(categorical) assert ak.to_list(array) == categorical.tolist() not_categorical = ak.from_categorical(categorical) assert not ak.is_categorical(not_categorical) assert ak.categories(categorical).tolist() == ["one", "two", "three"]
def test_to_categorical_numbers(): array = ak.Array([1.1, 2.2, 3.3, 1.1, 2.2, 3.3, 1.1, 2.2, 3.3]) assert not ak.is_categorical(array) categorical = ak.to_categorical(array) assert ak.is_categorical(categorical) assert ak.to_list(array) == categorical.tolist() assert ak.to_list(categorical.layout.content) == [1.1, 2.2, 3.3] not_categorical = ak.from_categorical(categorical) assert not ak.is_categorical(not_categorical) assert ak.categories(categorical).tolist() == [1.1, 2.2, 3.3]
def test_to_categorical(): array = ak.Array( ["one", "two", "three", "one", "two", "three", "one", "two", "three"]) assert not ak.is_categorical(array) categorical = ak.to_categorical(array) assert ak.is_categorical(categorical) assert ak.to_list(array) == categorical.tolist() assert ak.to_list(categorical.layout.content) == ["one", "two", "three"] not_categorical = ak.from_categorical(categorical) assert not ak.is_categorical(not_categorical) assert ak.categories(categorical).tolist() == ["one", "two", "three"]
def test_to_categorical_masked(): content = ak.Array([ "one", "two", "three", "one", "one", "two", "three", "two", "one", "two", "three", "three", ]).layout mask = ak.layout.Index8( np.array([ False, False, False, True, False, False, False, True, False, False, False, True, ])) array = ak.Array(ak.layout.ByteMaskedArray(mask, content, valid_when=False)) assert not ak.is_categorical(array) categorical = ak.to_categorical(array) assert ak.is_categorical(categorical) assert ak.to_list(array) == categorical.tolist() assert ak.to_list(categorical.layout.content) == ["one", "two", "three"] not_categorical = ak.from_categorical(categorical) assert not ak.is_categorical(not_categorical) assert ak.categories(categorical).tolist() == ["one", "two", "three"]
def test_zip(): x = ak.Array([1.1, 2.2, 3.3]) y = ak.Array(["one", "two", "three"]) assert ak.zip({ "x": x, "y": y }).tolist() == [ { "x": 1.1, "y": "one" }, { "x": 2.2, "y": "two" }, { "x": 3.3, "y": "three" }, ] y = ak.to_categorical(y) assert ak.zip({ "x": x, "y": y }).tolist() == [ { "x": 1.1, "y": "one" }, { "x": 2.2, "y": "two" }, { "x": 3.3, "y": "three" }, ]
def test_categorical_is_valid(): # validate a categorical array by its content arr = ak.Array([2019, 2020, 2021, 2020, 2019]) categorical = ak.to_categorical(arr) assert ak.is_valid(categorical)
def test_arraytype_categorical_2(): text = str( ak.to_categorical(ak.Array([1.1, 1.1, 2.2, 3.3, 1.1, 3.3])).type) parsedtype = ak.types.from_datashape(text, True) assert isinstance(parsedtype, ak.types.ArrayType) assert str(parsedtype) == text