Skip to content

Commit 14aadd3

Browse files
better errors for lazyframes (#1916)
Co-authored-by: Riccardo Cappuzzo <7548232+rcap107@users.noreply.github.com>
1 parent f41a171 commit 14aadd3

6 files changed

Lines changed: 19 additions & 23 deletions

File tree

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Bug Fixes
3838
provided encoders is a scikit-learn Pipeline that starts with a skrub
3939
single-column transformer. :pr:`1899` by :user:`Jérôme Dockès <jeromedockes>`
4040
and :pr:`1900` by :user:`Jérôme Dockès <jeromedockes>`.
41+
- Errors raised when a polars LazyFrame is passed where an eager DataFrame is
42+
expected are now clearer. :pr:`1916` by :user:`Jérôme Dockès <jeromedockes>`.
4143

4244
Release 0.7.2
4345
=============

skrub/_check_input.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,17 @@ def _check_not_pandas_sparse_pandas(df):
6464

6565

6666
def _check_is_dataframe(df):
67+
if sbd.is_lazyframe(df):
68+
warnings.warn(
69+
"At the moment, skrub only works on eager DataFrames, calling collect()."
70+
)
71+
df = sbd.collect(df)
6772
if not sbd.is_dataframe(df):
6873
raise TypeError(
6974
"Only pandas and polars DataFrames are supported. Cannot handle X of"
7075
f" type: {type(df)}."
7176
)
72-
73-
74-
def _collect_lazyframe(df):
75-
if not sbd.is_lazyframe(df):
76-
return df
77-
warnings.warn(
78-
"At the moment, skrub only works on eager DataFrames, calling collect()."
79-
)
80-
return sbd.collect(df)
77+
return df
8178

8279

8380
class CheckInputDataFrame(TransformerMixin, BaseEstimator):
@@ -122,7 +119,7 @@ def fit(self, X, y=None):
122119
def fit_transform(self, X, y=None):
123120
del y
124121
X = self._handle_array(X)
125-
_check_is_dataframe(X)
122+
X = _check_is_dataframe(X)
126123
self.module_name_ = sbd.dataframe_module_name(X)
127124
# TODO check schema (including dtypes) not just names.
128125
# Need to decide how strict we should be about types
@@ -133,13 +130,12 @@ def fit_transform(self, X, y=None):
133130
if sbd.column_names(X) != self.feature_names_out_:
134131
X = sbd.set_column_names(X, self.feature_names_out_)
135132
_check_not_pandas_sparse(X)
136-
X = _collect_lazyframe(X)
137133
return X
138134

139135
def transform(self, X):
140136
check_is_fitted(self, "module_name_")
141137
X = self._handle_array(X)
142-
_check_is_dataframe(X)
138+
X = _check_is_dataframe(X)
143139
module_name = sbd.dataframe_module_name(X)
144140
if module_name != self.module_name_:
145141
raise TypeError(
@@ -161,7 +157,6 @@ def transform(self, X):
161157
if sbd.column_names(X) != self.feature_names_out_:
162158
X = sbd.set_column_names(X, self.feature_names_out_)
163159
_check_not_pandas_sparse(X)
164-
X = _collect_lazyframe(X)
165160
return X
166161

167162
def _handle_array(self, X):

skrub/_dataframe/_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ def _shape_pandas(obj):
502502
return obj.shape
503503

504504

505-
@shape.specialize("polars")
505+
@shape.specialize("polars", argument_type=("DataFrame", "Column"))
506506
def _shape_polars(obj):
507507
return obj.shape
508508

skrub/_dispatch.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def _load_dataframe_module_info(name):
171171
"module": polars,
172172
"types": MappingProxyType(
173173
{
174-
"DataFrame": (polars.DataFrame, polars.LazyFrame),
174+
"DataFrame": (polars.DataFrame,),
175175
"LazyFrame": (polars.LazyFrame,),
176176
"EagerFrame": (polars.DataFrame,),
177177
"Column": (polars.Series,),
@@ -262,6 +262,12 @@ def raise_dispatch_unregistered_type(obj, kind="object"):
262262
evaluated) rather than computing it immediately.
263263
"""
264264
)
265+
if type(obj).__name__ == "LazyFrame":
266+
raise TypeError(
267+
"Expected a DataFrame but got an object "
268+
f"of type {type(obj)}. LazyFrames are not yet supported; "
269+
"please call collect() to convert to a DataFrame."
270+
)
265271
raise TypeError(
266272
"Operation not supported on this object. Expecting a Pandas or Polars "
267273
f"{kind}, but got an object of type {type(obj)}."

skrub/_reporting/_table_report.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,6 @@ def __init__(
242242
self.dataframe = (
243243
sbd.to_frame(dataframe) if sbd.is_column(dataframe) else dataframe
244244
)
245-
if sbd.is_polars(dataframe) and sbd.is_lazyframe(dataframe):
246-
raise ValueError(
247-
"The TableReport does not support lazy dataframes. Please call"
248-
" `.collect()` to use the TableReport on the current dataframe."
249-
)
250245
self.n_columns = sbd.shape(self.dataframe)[1]
251246

252247
def _set_minimal_mode(self):

skrub/_reporting/tests/test_table_report.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ def test_lazyframe_exception():
103103
pl = pytest.importorskip("polars")
104104
lazy_df = pl.DataFrame({"a": ["1", "2", "3"]}).lazy()
105105

106-
with pytest.raises(
107-
ValueError, match=r"TableReport does not support lazy dataframes"
108-
):
106+
with pytest.raises(TypeError, match=r".*LazyFrames are not yet supported"):
109107
TableReport(lazy_df)
110108

111109

0 commit comments

Comments
 (0)