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
15 changes: 5 additions & 10 deletions diffly/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from __future__ import annotations

import datetime as dt
import warnings
from collections.abc import Iterable, Mapping, Sequence
from functools import cached_property
from typing import TYPE_CHECKING, Literal, Self, overload
Expand Down Expand Up @@ -165,18 +164,14 @@ def _init_with_validation(
"The columns are not a primary key for the right data frame."
)

# Try joining empty frames to check if the primary key columns are
# compatible. If not, we set the primary key to `None` and emit an
# appropriate warning.
# Check that the primary key dtypes are compatible for joining.
try:
left_schema.to_frame().join(right_schema.to_frame(), on=primary_key)
except pl.exceptions.SchemaError as e:
warnings.warn(
"`primary_key` is set to None as the primary key of the left and "
"right tables have incompatible data types: "
+ str(e).split("\n")[0],
)
primary_key = None
raise PrimaryKeyError(
"Primary key columns have incompatible dtypes between left and "
"right: " + str(e).split("\n")[0]
) from e

# Assign other relevant attributes
schemas = Schemas(left_schema, right_schema)
Expand Down
5 changes: 2 additions & 3 deletions tests/test_dataframe_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ def test_pk_violation() -> None:


def test_incompatible_primary_key_dtypes() -> None:
with pytest.warns(UserWarning, match=".*datatypes of join keys don't match.*"):
comparison = compare_frames(
with pytest.raises(PrimaryKeyError, match="incompatible dtypes"):
compare_frames(
pl.DataFrame({"key": ["tiger"], "speed_kph": [5.0]}),
pl.DataFrame({"key": [1], "speed_kph": [5.0]}),
primary_key=["key"],
)
comparison.summary()


def test_incomplete_mapping() -> None:
Expand Down