diff --git a/CHANGES.rst b/CHANGES.rst index 1956c30bb..fde50f36a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -21,6 +21,9 @@ Changes removed in the next release of skrub. :pr:`1546` by :user:`Vincent Maladiere `. +- Improved error messages when a DataOp is being sent to dispatched functions. + :pr:`1607` by :user:`Riccardo Cappuzzo`. + Bugfixes -------- diff --git a/skrub/_dataframe/_common.py b/skrub/_dataframe/_common.py index c3725dee0..85272eac7 100644 --- a/skrub/_dataframe/_common.py +++ b/skrub/_dataframe/_common.py @@ -119,6 +119,18 @@ def _raise(obj, kind="object"): + from .._data_ops._data_ops import DataOp + + if isinstance(obj, DataOp): + raise TypeError( + """Expected a Pandas or Polars DataFrame, but got a skrub DataOp. +A function that expects an actual value cannot be applied directly to a DataOp; +you may want to (i) use op.skb.eval() or op.skb.preview() to evaluate the +dataop and turn it into an actual value or (ii) use op.skb.apply_func() or +op.skb.apply() to schedule the operation for later execution (when the dataop is +evaluated) rather than computing it immediately. + """ + ) raise TypeError( "Operation not supported on this object. Expecting a Pandas or Polars " f"{kind}, but got an object of type {type(obj)}." diff --git a/skrub/_dataframe/tests/test_common.py b/skrub/_dataframe/tests/test_common.py index 2b1fe6a88..b0db4de4f 100644 --- a/skrub/_dataframe/tests/test_common.py +++ b/skrub/_dataframe/tests/test_common.py @@ -15,6 +15,7 @@ from numpy.testing import assert_array_equal from pandas.testing import assert_frame_equal as pd_assert_frame_equal +import skrub from skrub import selectors as s from skrub._dataframe import _common as ns @@ -40,6 +41,12 @@ def test_not_implemented(): params = [None] * n_params with pytest.raises(TypeError): func(*params) + dop = [skrub.var("a")] * n_params + with pytest.raises( + TypeError, + match=r"Expected a Pandas or Polars DataFrame, but got a skrub DataOp", + ): + func(*dop) #