Skip to content

Commit 4bb957b

Browse files
authored
feat: Show user_rows in MaxRowsError message (#3768)
1 parent 8a6f65c commit 4bb957b

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

altair/utils/data.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,26 @@ def consolidate_datasets(self, value: bool) -> None:
106106
class MaxRowsError(Exception):
107107
"""Raised when a data model has too many rows."""
108108

109+
def __init__(self, message: str, /) -> None:
110+
self.message = message
111+
super().__init__(self.message)
112+
113+
@classmethod
114+
def from_limit_rows(cls, user_rows: int, max_rows: int, /) -> MaxRowsError:
115+
msg = (
116+
f"The number of rows in your dataset ({user_rows}) is greater "
117+
f"than the maximum allowed ({max_rows}).\n\n"
118+
"Try enabling the VegaFusion data transformer which "
119+
"raises this limit by pre-evaluating data\n"
120+
"transformations in Python.\n"
121+
" >> import altair as alt\n"
122+
' >> alt.data_transformers.enable("vegafusion")\n\n'
123+
"Or, see https://altair-viz.github.io/user_guide/large_datasets.html "
124+
"for additional information\n"
125+
"on how to plot large datasets."
126+
)
127+
return cls(msg)
128+
109129

110130
@overload
111131
def limit_rows(data: None = ..., max_rows: int | None = ...) -> partial: ...
@@ -123,21 +143,6 @@ def limit_rows(
123143
return partial(limit_rows, max_rows=max_rows)
124144
check_data_type(data)
125145

126-
def raise_max_rows_error():
127-
msg = (
128-
"The number of rows in your dataset is greater "
129-
f"than the maximum allowed ({max_rows}).\n\n"
130-
"Try enabling the VegaFusion data transformer which "
131-
"raises this limit by pre-evaluating data\n"
132-
"transformations in Python.\n"
133-
" >> import altair as alt\n"
134-
' >> alt.data_transformers.enable("vegafusion")\n\n'
135-
"Or, see https://altair-viz.github.io/user_guide/large_datasets.html "
136-
"for additional information\n"
137-
"on how to plot large datasets."
138-
)
139-
raise MaxRowsError(msg)
140-
141146
if isinstance(data, SupportsGeoInterface):
142147
if data.__geo_interface__["type"] == "FeatureCollection":
143148
values = data.__geo_interface__["features"]
@@ -152,8 +157,9 @@ def raise_max_rows_error():
152157
data = to_eager_narwhals_dataframe(data)
153158
values = data
154159

155-
if max_rows is not None and len(values) > max_rows:
156-
raise_max_rows_error()
160+
n = len(values)
161+
if max_rows is not None and n > max_rows:
162+
raise MaxRowsError.from_limit_rows(n, max_rows)
157163

158164
return data
159165

0 commit comments

Comments
 (0)