Skip to content

Commit 9f793ba

Browse files
authored
Adding a special case to _common so that Data Ops get a more informative message (#1607)
1 parent 2386766 commit 9f793ba

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Changes
2121
removed in the next release of skrub.
2222
:pr:`1546` by :user:`Vincent Maladiere <Vincent-Maladiere>`.
2323

24+
- Improved error messages when a DataOp is being sent to dispatched functions.
25+
:pr:`1607` by :user:`Riccardo Cappuzzo<rcap107>`.
26+
2427
Bugfixes
2528
--------
2629

skrub/_dataframe/_common.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@
119119

120120

121121
def _raise(obj, kind="object"):
122+
from .._data_ops._data_ops import DataOp
123+
124+
if isinstance(obj, DataOp):
125+
raise TypeError(
126+
"""Expected a Pandas or Polars DataFrame, but got a skrub DataOp.
127+
A function that expects an actual value cannot be applied directly to a DataOp;
128+
you may want to (i) use op.skb.eval() or op.skb.preview() to evaluate the
129+
dataop and turn it into an actual value or (ii) use op.skb.apply_func() or
130+
op.skb.apply() to schedule the operation for later execution (when the dataop is
131+
evaluated) rather than computing it immediately.
132+
"""
133+
)
122134
raise TypeError(
123135
"Operation not supported on this object. Expecting a Pandas or Polars "
124136
f"{kind}, but got an object of type {type(obj)}."

skrub/_dataframe/tests/test_common.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from numpy.testing import assert_array_equal
1616
from pandas.testing import assert_frame_equal as pd_assert_frame_equal
1717

18+
import skrub
1819
from skrub import selectors as s
1920
from skrub._dataframe import _common as ns
2021

@@ -40,6 +41,12 @@ def test_not_implemented():
4041
params = [None] * n_params
4142
with pytest.raises(TypeError):
4243
func(*params)
44+
dop = [skrub.var("a")] * n_params
45+
with pytest.raises(
46+
TypeError,
47+
match=r"Expected a Pandas or Polars DataFrame, but got a skrub DataOp",
48+
):
49+
func(*dop)
4350

4451

4552
#

0 commit comments

Comments
 (0)