diff --git a/polars/polars-core/src/frame/asof_join/mod.rs b/polars/polars-core/src/frame/asof_join/mod.rs index 7271678d8186..f6f84c13b1e8 100644 --- a/polars/polars-core/src/frame/asof_join/mod.rs +++ b/polars/polars-core/src/frame/asof_join/mod.rs @@ -30,6 +30,11 @@ pub struct AsOfOptions { fn check_asof_columns(a: &Series, b: &Series, check_sorted: bool) -> PolarsResult<()> { let dtype_a = a.dtype(); let dtype_b = b.dtype(); + polars_ensure!( + dtype_a.to_physical().is_numeric() && dtype_b.to_physical().is_numeric(), + InvalidOperation: + "asof join only supported on numeric/temporal keys" + ); polars_ensure!( dtype_a == dtype_b, ComputeError: "mismatching key dtypes in asof-join: `{}` and `{}`", diff --git a/py-polars/tests/unit/operations/test_join_asof.py b/py-polars/tests/unit/operations/test_join_asof.py index bf99943a520a..9a86ca28cf21 100644 --- a/py-polars/tests/unit/operations/test_join_asof.py +++ b/py-polars/tests/unit/operations/test_join_asof.py @@ -2,6 +2,7 @@ from typing import Any import numpy as np +import pytest import polars as pl from polars.testing import assert_frame_equal @@ -486,3 +487,12 @@ def test_asof_join_nearest_by_date() -> None: out = df1.join_asof(df2, on="asof_key", by="group", strategy="nearest") assert_frame_equal(out, expected) + + +def test_asof_join_string_err() -> None: + left = pl.DataFrame({"date_str": ["2023/02/15"]}).sort("date_str") + right = pl.DataFrame( + {"date_str": ["2023/01/31", "2023/02/28"], "value": [0, 1]} + ).sort("date_str") + with pytest.raises(pl.InvalidOperationError): + left.join_asof(right, on="date_str")