Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: bump polars to 1.9.0, fix breaks in search and summaries (#2553)

Myles Scolnick2014f45a3565
diff --git a/frontend/src/components/data-table/types.ts b/frontend/src/components/data-table/types.ts
index 99a32dd8ae..8bc9e71d51 100644
--- a/frontend/src/components/data-table/types.ts
+++ b/frontend/src/components/data-table/types.ts
@@ -6,7 +6,7 @@ export interface ColumnHeaderSummary {
column: string | number;
min?: number | string | undefined | null;
max?: number | string | undefined | null;
- unique?: number | null;
+ unique?: number | unknown[] | undefined | null;
nulls?: number | null;
true?: number | null;
false?: number | null;
diff --git a/frontend/src/plugins/impl/DataTablePlugin.tsx b/frontend/src/plugins/impl/DataTablePlugin.tsx
index 99b1427d39..31bb5dde23 100644
--- a/frontend/src/plugins/impl/DataTablePlugin.tsx
+++ b/frontend/src/plugins/impl/DataTablePlugin.tsx
@@ -138,7 +138,7 @@ export const DataTablePlugin = createPlugin<S>("marimo-table")
column: z.union([z.number(), z.string()]),
min: z.union([z.number(), z.string()]).nullish(),
max: z.union([z.number(), z.string()]).nullish(),
- unique: z.number().nullish(),
+ unique: z.union([z.number(), z.array(z.any())]).nullish(),
nulls: z.number().nullish(),
true: z.number().nullish(),
false: z.number().nullish(),
diff --git a/frontend/src/utils/numbers.ts b/frontend/src/utils/numbers.ts
index f5b0913da9..0cfb7e943f 100644
--- a/frontend/src/utils/numbers.ts
+++ b/frontend/src/utils/numbers.ts
@@ -1,12 +1,16 @@
/* Copyright 2024 Marimo. All rights reserved. */
export function prettyNumber(
- value: number | string | undefined | null | boolean,
+ value: number | string | undefined | null | boolean | unknown[],
): string {
if (value === undefined || value === null) {
return "";
}
+ if (Array.isArray(value)) {
+ return String(value);
+ }
+
if (typeof value === "string") {
return value;
}
diff --git a/marimo/_plugins/core/json_encoder.py b/marimo/_plugins/core/json_encoder.py
index 440d60fca9..ca7a4305d8 100644
--- a/marimo/_plugins/core/json_encoder.py
+++ b/marimo/_plugins/core/json_encoder.py
@@ -120,6 +120,16 @@ class WebComponentEncoder(JSONEncoder):
except AttributeError:
pass
+ # Handle polars objects
+ if DependencyManager.polars.imported():
+ import polars as pl
+
+ if isinstance(o, pl.DataFrame):
+ return o.to_dict()
+
+ if isinstance(o, pl.Series):
+ return o.to_list()
+
# Handle named tuples
if isinstance(o, tuple) and hasattr(o, "_fields"):
return {
diff --git a/marimo/_plugins/ui/_impl/tables/polars_table.py b/marimo/_plugins/ui/_impl/tables/polars_table.py
index fd4c6eb861..9acf67cbff 100644
--- a/marimo/_plugins/ui/_impl/tables/polars_table.py
+++ b/marimo/_plugins/ui/_impl/tables/polars_table.py
@@ -142,10 +142,27 @@ class PolarsTableManagerFactory(TableManagerFactory):
def search(self, query: str) -> TableManager[Any]:
query = query.lower()
- expressions = [
- pl.col(column).str.contains(f"(?i){query}")
- for column in self.data.columns
- ]
+ expressions = []
+ for column in self.data.columns:
+ dtype = self.data[column].dtype
+ if dtype == pl.String:
+ expressions.append(pl.col(column).str.contains(query))
+ elif dtype == pl.List(pl.Utf8):
+ expressions.append(pl.col(column).list.contains(query))
+ elif (
+ dtype.is_numeric()
+ or dtype.is_temporal()
+ or dtype == pl.Boolean
+ ):
+ expressions.append(
+ pl.col(column)
+ .cast(pl.String)
+ .str.contains(f"(?i){query}")
+ )
+
+ if not expressions:
+ return PolarsTableManager(self.data.filter(pl.lit(False)))
+
or_expr = expressions[0]
for expr in expressions[1:]:
or_expr = or_expr | expr
@@ -199,6 +216,11 @@ class PolarsTableManagerFactory(TableManagerFactory):
total=total,
nulls=col.null_count(),
)
+ if col.dtype == pl.List:
+ return ColumnSummary(
+ total=total,
+ nulls=col.null_count(),
+ )
return ColumnSummary(
total=total,
nulls=col.null_count(),
diff --git a/marimo/_smoke_tests/issues/2550-search-polars.py b/marimo/_smoke_tests/issues/2550-search-polars.py
new file mode 100644
index 0000000000..e13373ef81
--- /dev/null
+++ b/marimo/_smoke_tests/issues/2550-search-polars.py
@@ -0,0 +1,39 @@
+import marimo
+
+__generated_with = "0.9.4"
+app = marimo.App(width="medium")
+
+
+def __():
+ import marimo as mo
+ import polars as pl
+ import pandas
+
+ data = pl.DataFrame(
+ {
+ "A": [1, 2, 3],
+ "B": ["foo", "bar", "baz"],
+ "C": [True, False, True],
+ "D": [["zz", "yyy"], [], []],
+ "E": [1.1, 2.2, 3.3],
+ "F": [[12, 34], [], []],
+ }
+ )
+ return data, mo, pandas, pl
+
+
+def __(data):
+ data.to_pandas()
+ return
+
+
+def __(data):
+ data
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/pyproject.toml b/pyproject.toml
index 0c4797ce63..8d12bee920 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -142,7 +142,7 @@ installer = "uv"
dependencies = [
"mypy~=1.10.1",
# Types for mypy
- "polars~=1.5.0",
+ "polars~=1.9.0",
"pyarrow-stubs>=17.0",
"pandas-stubs>=1.5.3.230321",
"types-Pillow~=10.2.0.20240520",
@@ -181,7 +181,10 @@ extra-dependencies = [
"pytest-codecov~=0.5.1",
"pytest-asyncio~=0.23.8",
# For testing mo.ui.chart, table, ...
+ "pyarrow",
+ "pyarrow_hotfix",
"altair>=5.4.0",
+ "polars~=1.9.0",
"pandas>=1.5.3",
"geopandas~=0.14.4; python_version > '3.9'",
# For testing mo.image
@@ -315,7 +318,7 @@ docstring-code-format = true
ban-relative-imports = "all"
# Ban certain modules from being imported at module level, instead requiring
# that they're imported lazily (e.g., within a function definition).
-banned-module-level-imports = ["numpy", "pandas", "tomlkit", "polars"]
+banned-module-level-imports = ["numpy", "pandas", "tomlkit", "polars", "altair", "ipython", "anywidget"]
[tool.ruff.lint.flake8-pytest-style]
mark-parentheses = false
diff --git a/tests/_cli/snapshots/ipynb_to_marimo.txt b/tests/_cli/snapshots/ipynb_to_marimo.txt
index 3e1e3c2757..8cd2900fc0 100644
--- a/tests/_cli/snapshots/ipynb_to_marimo.txt
+++ b/tests/_cli/snapshots/ipynb_to_marimo.txt
@@ -1,6 +1,6 @@
import marimo
-__generated_with = "0.8.22"
+__generated_with = "0.9.4"
app = marimo.App()
diff --git a/tests/_cli/snapshots/ipynb_to_marimo_with_output.txt b/tests/_cli/snapshots/ipynb_to_marimo_with_output.txt
index 724669e8b8..58b33e5492 100644
--- a/tests/_cli/snapshots/ipynb_to_marimo_with_output.txt
+++ b/tests/_cli/snapshots/ipynb_to_marimo_with_output.txt
@@ -1,6 +1,6 @@
import marimo
-__generated_with = "0.8.22"
+__generated_with = "0.9.4"
app = marimo.App()
diff --git a/tests/_cli/snapshots/markdown_to_marimo.txt b/tests/_cli/snapshots/markdown_to_marimo.txt
index d61e276677..15560a3698 100644
--- a/tests/_cli/snapshots/markdown_to_marimo.txt
+++ b/tests/_cli/snapshots/markdown_to_marimo.txt
@@ -1,6 +1,6 @@
import marimo
-__generated_with = "0.8.22"
+__generated_with = "0.9.4"
app = marimo.App()
diff --git a/tests/_plugins/core/test_json_encoder.py b/tests/_plugins/core/test_json_encoder.py
index e00ff619f3..2630415630 100644
--- a/tests/_plugins/core/test_json_encoder.py
+++ b/tests/_plugins/core/test_json_encoder.py
@@ -13,7 +13,11 @@ from marimo._dependencies.dependencies import DependencyManager
from marimo._output.mime import MIME
from marimo._plugins.core.json_encoder import WebComponentEncoder
-HAS_DEPS = DependencyManager.pandas.has() and DependencyManager.altair.has()
+HAS_DEPS = (
+ DependencyManager.pandas.has()
+ and DependencyManager.altair.has()
+ and DependencyManager.polars.has()
+)
@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")
@@ -90,6 +94,19 @@ def test_pandas_encoding() -> None:
assert encoded_other == '["a", "b", "c"]'
[email protected](not HAS_DEPS, reason="optional dependencies not installed")
+def test_polars_encoding() -> None:
+ import polars as pl
+
+ df = pl.DataFrame({"a": [1, 2], "b": [3, 4]})
+ encoded = json.dumps(df, cls=WebComponentEncoder)
+ assert encoded == '{"a": [1, 2], "b": [3, 4]}'
+
+ series = pl.Series([1, 2, 3])
+ encoded_series = json.dumps(series, cls=WebComponentEncoder)
+ assert encoded_series == "[1, 2, 3]"
+
+
@dataclass
class MockMIMEObject:
def _mime_(self) -> tuple[str, str]:
diff --git a/tests/_plugins/ui/_impl/dataframes/test_handlers.py b/tests/_plugins/ui/_impl/dataframes/test_handlers.py
index a112485575..138274fa90 100644
--- a/tests/_plugins/ui/_impl/dataframes/test_handlers.py
+++ b/tests/_plugins/ui/_impl/dataframes/test_handlers.py
@@ -860,6 +860,7 @@ class TestTransformHandler:
"B": [1, 1, 1, 1],
"C": [["a", "b", "c"], [np.nan], [], ["d", "e"]],
},
+ strict=False,
),
ibis.memtable(
{
diff --git a/tests/_plugins/ui/_impl/tables/test_polars_table.py b/tests/_plugins/ui/_impl/tables/test_polars_table.py
index e47d13ef05..5810324e8f 100644
--- a/tests/_plugins/ui/_impl/tables/test_polars_table.py
+++ b/tests/_plugins/ui/_impl/tables/test_polars_table.py
@@ -326,13 +326,26 @@ class TestPolarsTableManagerFactory(unittest.TestCase):
"A": [1, 2, 3],
"B": ["foo", "bar", "baz"],
"C": [True, False, True],
+ "D": [["zz", "yyy"], [], []],
+ "E": [1.1, 2.2, 3.3],
}
)
manager = self.factory.create()(df)
+ # Exact match
assert manager.search("foo").get_num_rows() == 1
+ # Contains
assert manager.search("a").get_num_rows() == 2
+ # Case insensitive / boolean
assert manager.search("true").get_num_rows() == 2
+ # Overmatch
assert manager.search("food").get_num_rows() == 0
+ # Int (exact match)
+ assert manager.search("1").get_num_rows() == 1
+ # Float (exact match)
+ assert manager.search("1.1").get_num_rows() == 1
+ # List (exact match)
+ assert manager.search("yyy").get_num_rows() == 1
+ assert manager.search("y").get_num_rows() == 0
def test_apply_formatting_does_not_modify_original_data(self) -> None:
from polars.testing import assert_frame_equal
diff --git a/tests/_plugins/ui/_impl/test_altair_chart.py b/tests/_plugins/ui/_impl/test_altair_chart.py
index 874e6fc058..3a429f345e 100644
--- a/tests/_plugins/ui/_impl/test_altair_chart.py
+++ b/tests/_plugins/ui/_impl/test_altair_chart.py
@@ -27,7 +27,6 @@ HAS_DEPS = (
DependencyManager.pandas.has()
and DependencyManager.polars.has()
and DependencyManager.altair.has()
- and DependencyManager.geopandas.has()
# altair produces different output on windows
and sys.platform != "win32"
)
@@ -352,7 +351,10 @@ def test_parse_spec_narwhal() -> None:
snapshot("parse_spec_narwhal.txt", json.dumps(spec, indent=2))
[email protected](not HAS_DEPS, reason="optional dependencies not installed")
+ not HAS_DEPS or not DependencyManager.geopandas.has(),
+ reason="optional dependencies not installed",
+)
def test_parse_spec_geopandas() -> None:
import altair as alt
import geopandas as gpd