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_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"]