Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion dataframely/columns/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,16 @@ def dtype(self) -> pl.DataType:
return pl.Array(self.inner.dtype, self.shape)

def validation_rules(self, expr: pl.Expr) -> dict[str, pl.Expr]:
def all_elements(inner_expr: pl.Expr) -> pl.Expr:
# The elements of a multi-dimensional array are arrays themselves, so the
# inner rules must be applied one dimension further down for every extra
# dimension.
for _ in range(len(self.shape) - 1):
inner_expr = pl.element().arr.eval(inner_expr).arr.all()
return expr.arr.eval(inner_expr).arr.all()

inner_rules = {
f"inner_{rule_name}": expr.arr.eval(inner_expr).arr.all()
f"inner_{rule_name}": all_elements(inner_expr)
for rule_name, inner_expr in self.inner.validation_rules(
pl.element()
).items()
Expand Down
73 changes: 73 additions & 0 deletions tests/column_types/test_array.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright (c) QuantCo 2025-2026
# SPDX-License-Identifier: BSD-3-Clause

from typing import Any

import polars as pl
import pytest

Expand Down Expand Up @@ -145,6 +147,77 @@ def test_array_with_rules() -> None:
assert failures.counts() == {"a|inner_nullability": 1, "a|inner_min_length": 1}


@pytest.mark.parametrize(
("column", "prefix"),
[
(
dy.Array(dy.String(min_length=2, nullable=False), (2, 2), nullable=True),
"inner",
),
# A multi-dimensional array must behave like the equivalent nested arrays
(
dy.Array(
dy.Array(dy.String(min_length=2, nullable=False), 2), 2, nullable=True
),
"inner_inner",
),
],
)
def test_multidimensional_array_with_rules(column: Column, prefix: str) -> None:
# Arrange
schema = create_schema("test", {"a": column})
df = pl.DataFrame(
{
"a": [
[["ab", "cd"], ["ef", "gh"]],
[["ab", "c"], ["ef", "gh"]],
[["ab", "cd"], [None, "gh"]],
None,
]
},
schema={"a": pl.Array(pl.String, (2, 2))},
)

# Act
_, failures = schema.filter(df)

# Assert
assert validation_mask(df, failures).to_list() == [True, False, False, True]
assert failures.counts() == {
f"a|{prefix}_nullability": 1,
f"a|{prefix}_min_length": 1,
}


@pytest.mark.parametrize(
("shape", "data"),
[
((2, 3), [[[0, 1, 2], [3, 4, 5]], [[0, 1, 2], [3, -4, 5]]]),
# Three dimensions wrap the inner rule twice.
(
(2, 2, 2),
[
[[[0, 1], [2, 3]], [[4, 5], [6, 7]]],
[[[0, 1], [2, 3]], [[4, 5], [6, -7]]],
],
),
],
)
def test_multidimensional_array_with_numeric_rules(
shape: tuple[int, ...], data: list[Any]
) -> None:
# Arrange
schema = create_schema("test", {"a": dy.Array(dy.Int64(min=0), shape)})
df = pl.DataFrame({"a": data}, schema={"a": pl.Array(pl.Int64, shape)})

# Act
_, failures = schema.filter(df)

# Assert
assert validation_mask(df, failures).to_list() == [True, False]
assert failures.counts() == {"a|inner_min": 1}


def test_array_with_pk() -> None:
schema = create_schema(
"test",
Expand Down
Loading