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
14 changes: 14 additions & 0 deletions dataframely/columns/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,16 @@ def _attributes_match(
) -> bool:
if name == "check":
return _compare_checks(lhs, rhs, column_expr)

lhs_is_series = isinstance(lhs, pl.Series)
rhs_is_series = isinstance(rhs, pl.Series)

if lhs_is_series != rhs_is_series:
return False

if lhs_is_series and rhs_is_series:
return _compare_series(lhs, rhs)

return lhs == rhs

# -------------------------------- DUNDER METHODS -------------------------------- #
Expand All @@ -401,6 +411,10 @@ def __str__(self) -> str:
return self.__class__.__name__.lower()


def _compare_series(lhs: pl.Series, rhs: pl.Series) -> bool:
return (len(lhs) == len(rhs)) and lhs.equals(rhs)


def _compare_checks(lhs: Check | None, rhs: Check | None, expr: pl.Expr) -> bool:
match (lhs, rhs):
case (None, None):
Expand Down
12 changes: 12 additions & 0 deletions tests/columns/test_matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,19 @@
dy.Datetime(time_zone=dt.timezone(dt.timedelta(hours=0))),
True,
),
(dy.Enum(["a", "b"]), dy.Enum(["a", "b"]), True),
(dy.Enum(["a", "b"]), dy.Enum(["a", "b", "c"]), False),
],
)
def test_matches(lhs: dy.Column, rhs: dy.Column, expected: bool) -> None:
assert lhs.matches(rhs, expr=pl.element()) == expected


def test_matches_enum_attribute_type_mismatch() -> None:
# Comparison should fail if the `other` column has
# a `category` member, but its dtype is not `pl.Series`
col1 = dy.Enum(["a", "b"])
col2 = dy.Enum(["a", "b"])
col2.categories = "this_is_not_a_series" # type: ignore

assert not col1.matches(col2, pl.element())
Loading