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 polars date comparison (#7176)

Shahmir Varqhad96aaee6e421
diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py b/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
index b39f37396f..b680c49fc8 100644
--- a/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
+++ b/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
@@ -1,7 +1,8 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
-from typing import TYPE_CHECKING
+import datetime
+from typing import TYPE_CHECKING, Any, Callable
import narwhals.stable.v2 as nw
from narwhals.stable.v2 import col
@@ -32,7 +33,9 @@ from marimo._plugins.ui._impl.dataframes.transforms.types import (
from marimo._utils.assert_never import assert_never
if TYPE_CHECKING:
+ import polars as pl
from narwhals.expr import Expr
+ from typing_extensions import TypeIs
__all__ = [
@@ -117,12 +120,33 @@ class NarwhalsTransformHandler(TransformHandler[DataFrame]):
filter_expr: nw.Expr | None = None
+ def convert_value(v: Any, converter: Callable[[str], Any]) -> Any:
+ # Convert a value whether it's a list or single value
+ if isinstance(v, (tuple, list)):
+ return [converter(str(item)) for item in v]
+ return converter(str(v))
+
for condition in transform.where:
# Don't convert to string if already a string or int
# Narwhals col() can handle both strings and integers
column = col(condition.column_id)
value = condition.value
+ # For polars, we need to convert the values based on dtype
+ native_df = df.to_native()
+ if _is_polars_dataframe_or_lazyframe(native_df):
+ import polars as pl
+
+ dtype = native_df.collect_schema()[str(condition.column_id)]
+ if dtype == pl.Datetime:
+ value = convert_value(
+ value, datetime.datetime.fromisoformat
+ )
+ elif dtype == pl.Date:
+ value = convert_value(value, datetime.date.fromisoformat)
+ elif dtype == pl.Time:
+ value = convert_value(value, datetime.time.fromisoformat)
+
# Build the expression based on the operator
condition_expr: nw.Expr
if condition.operator == "==":
@@ -344,3 +368,11 @@ class NarwhalsTransformHandler(TransformHandler[DataFrame]):
# In case it is not a SQL backend
return None
return None
+
+
+def _is_polars_dataframe_or_lazyframe(
+ df: Any,
+) -> TypeIs[pl.DataFrame | pl.LazyFrame]:
+ return nw.dependencies.is_polars_dataframe(
+ df
+ ) or nw.dependencies.is_polars_lazyframe(df)
diff --git a/marimo/_smoke_tests/pandas/pandas_nan.py b/marimo/_smoke_tests/pandas/pandas_nan.py
index 19b340ac22..3a21a477ff 100644
--- a/marimo/_smoke_tests/pandas/pandas_nan.py
+++ b/marimo/_smoke_tests/pandas/pandas_nan.py
@@ -9,7 +9,7 @@
import marimo
-__generated_with = "0.15.5"
+__generated_with = "0.17.6"
app = marimo.App(width="medium")
@@ -20,8 +20,18 @@ def _():
import numpy as np
# This shouldn't print a runtime warning
- df = pd.DataFrame({"a": [1,2,3], "b": [np.nan, np.nan, np.nan]})
+ df = pd.DataFrame({"a": [1, 2, 3], "b": [np.nan, np.nan, np.nan]})
df
+ return mo, np, pd
+
+
+def _(mo, np, pd):
+ i = np.random.randint(10000)
+ size = 12
+ # Prints a runtime warning, but still displays correctly
+ nan_df = pd.DataFrame({"id": [i] * size, "all_nan_col": [np.nan] * size})
+ mo.ui.table(nan_df)
return
diff --git a/marimo/_smoke_tests/tables/complex_types.py b/marimo/_smoke_tests/tables/complex_types.py
index 83975e52d0..a86284ee28 100644
--- a/marimo/_smoke_tests/tables/complex_types.py
+++ b/marimo/_smoke_tests/tables/complex_types.py
@@ -1,6 +1,6 @@
import marimo
-__generated_with = "0.17.2"
+__generated_with = "0.17.6"
app = marimo.App(width="medium")
@@ -174,15 +174,13 @@ def _(df, mo):
@app.cell
def _(mo):
- mo.md(
- r"""
+ mo.md(r"""
## Stress testing
We can use a large dataset to stress test our rendering, charting. Notebook credit to Vincent: [WoW Dataset](https://github.com/koaning/wow-avatar-datasets)
~36 million rows, 7 columns
- """
- )
+ """)
return
@@ -196,6 +194,5 @@ def _():
return
-
if __name__ == "__main__":
app.run()
diff --git a/tests/_plugins/ui/_impl/dataframes/test_handlers.py b/tests/_plugins/ui/_impl/dataframes/test_handlers.py
index cb73c602a1..eff7b7d77d 100644
--- a/tests/_plugins/ui/_impl/dataframes/test_handlers.py
+++ b/tests/_plugins/ui/_impl/dataframes/test_handlers.py
@@ -1113,3 +1113,49 @@ class TestTransformHandler:
)
# Check that the transformations were applied correctly
assert_frame_equal(result, expected)
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes(
+ {"date": [date(2001, 1, 1), date(2001, 1, 2)]},
+ exclude=["pyarrow"],
+ ),
+ create_test_dataframes(
+ {"date": [date(2001, 1, 1)]}, exclude=["pyarrow"]
+ ),
+ )
+ ),
+ )
+ def test_filter_rows_date(
+ df: DataFrameType, expected: DataFrameType
+ ) -> None:
+ eq_transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=[
+ Condition(
+ column_id="date",
+ operator="==",
+ value="2001-01-01",
+ )
+ ],
+ )
+ result = apply(df, eq_transform)
+ assert_frame_equal(result, expected)
+
+ in_transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=[
+ Condition(
+ column_id="date",
+ operator="in",
+ value=["2001-01-01"],
+ )
+ ],
+ )
+ result = apply(df, in_transform)
+ assert_frame_equal(result, expected)