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.

improvement: enhance lazy df utilities and ensure dataframe type preservation in transformations (#7401)

Myles Scolnickbbea8c7933f4
diff --git a/marimo/_plugins/ui/_impl/altair_chart.py b/marimo/_plugins/ui/_impl/altair_chart.py
index 15327511e1..59191370dd 100644
--- a/marimo/_plugins/ui/_impl/altair_chart.py
+++ b/marimo/_plugins/ui/_impl/altair_chart.py
@@ -32,6 +32,7 @@ from marimo._utils.narwhals_utils import (
can_narwhalify,
empty_df,
is_narwhals_lazyframe,
+ make_lazy,
)
LOGGER = _loggers.marimo_logger()
@@ -122,9 +123,7 @@ def _filter_dataframe(
binned_fields: Optional[dict[str, Any]] = None,
) -> Union[IntoDataFrame, IntoLazyFrame]:
# Use lazy evaluation for efficient chained filtering
- base = nw.from_native(native_df)
- is_lazy = is_narwhals_lazyframe(base)
- df = base.lazy()
+ df, undo_df = make_lazy(native_df)
if not isinstance(selection, dict):
raise TypeError("Input 'selection' must be a dictionary")
@@ -260,11 +259,7 @@ def _filter_dataframe(
# Continue without this filter - don't break the entire operation
continue
- if not is_lazy and is_narwhals_lazyframe(df):
- # Undo the lazy
- return df.collect().to_native() # type: ignore[no-any-return]
-
- return df.to_native()
+ return undo_df(df)
def _resolve_values(values: Any, dtype: Any) -> list[Any]:
diff --git a/marimo/_plugins/ui/_impl/charts/altair_transformer.py b/marimo/_plugins/ui/_impl/charts/altair_transformer.py
index 690a7287fd..efc7130613 100644
--- a/marimo/_plugins/ui/_impl/charts/altair_transformer.py
+++ b/marimo/_plugins/ui/_impl/charts/altair_transformer.py
@@ -15,7 +15,10 @@ from marimo._plugins.ui._impl.tables.utils import (
get_table_manager_or_none,
)
from marimo._utils.data_uri import build_data_url
-from marimo._utils.narwhals_utils import can_narwhalify, is_narwhals_lazyframe
+from marimo._utils.narwhals_utils import (
+ can_narwhalify,
+ make_lazy,
+)
LOGGER = _loggers.marimo_logger()
@@ -153,31 +156,22 @@ def _maybe_sanitize_dataframe(data: Any) -> Any:
def sanitize_nan_infs(data: Any) -> Any:
"""Sanitize NaN and Inf values in Dataframes for JSON serialization."""
if can_narwhalify(data):
- narwhals_data = nw.from_native(data)
- is_prev_lazy = is_narwhals_lazyframe(narwhals_data)
-
- # Convert to lazy for optimization if not already lazy
- if not is_prev_lazy:
- narwhals_data = narwhals_data.lazy()
+ df, undo = make_lazy(data)
# Get schema without collecting
- schema = narwhals_data.collect_schema()
+ schema = df.collect_schema()
for col, dtype in schema.items():
# Only numeric columns can have NaN or Inf values
if dtype.is_numeric():
- narwhals_data = narwhals_data.with_columns(
+ df = df.with_columns(
nw.when(nw.col(col).is_nan() | ~nw.col(col).is_finite())
.then(None)
.otherwise(nw.col(col))
.name.keep()
)
- # Collect if input was eager
- if not is_prev_lazy and is_narwhals_lazyframe(narwhals_data):
- narwhals_data = narwhals_data.collect()
-
- return narwhals_data.to_native()
+ return undo(df)
return data
diff --git a/marimo/_plugins/ui/_impl/dataframes/dataframe.py b/marimo/_plugins/ui/_impl/dataframes/dataframe.py
index 1b398d7b72..a16cd5574d 100644
--- a/marimo/_plugins/ui/_impl/dataframes/dataframe.py
+++ b/marimo/_plugins/ui/_impl/dataframes/dataframe.py
@@ -5,7 +5,6 @@ import inspect
import sys
from dataclasses import dataclass
from typing import (
- TYPE_CHECKING,
Any,
Callable,
Final,
@@ -14,8 +13,6 @@ from typing import (
Union,
)
-import narwhals.stable.v2 as nw
-
from marimo._output.rich_help import mddoc
from marimo._plugins.ui._core.ui_element import UIElement
from marimo._plugins.ui._impl.dataframes.transforms.apply import (
@@ -47,12 +44,9 @@ from marimo._plugins.validators import (
)
from marimo._runtime.functions import EmptyArgs, Function
from marimo._utils.memoize import memoize_last_value
-from marimo._utils.narwhals_utils import is_narwhals_lazyframe
+from marimo._utils.narwhals_utils import make_lazy
from marimo._utils.parse_dataclass import parse_raw
-if TYPE_CHECKING:
- from narwhals.typing import IntoLazyFrame
-
@dataclass
class GetDataFrameResponse:
@@ -145,10 +139,8 @@ class dataframe(UIElement[dict[str, Any], DataFrameType]):
except Exception:
pass
- # Make the dataframe lazy and keep track of whether it was lazy originally
- nw_df: nw.LazyFrame[Any] = nw.from_native(df, pass_through=False)
- self._was_lazy = is_narwhals_lazyframe(nw_df)
- nw_df = nw_df.lazy()
+ # Make the dataframe lazy and keep an undo callback to restore original type
+ nw_df, self._undo = make_lazy(df)
self._limit = limit
self._dataframe_name = dataframe_name
@@ -257,22 +249,20 @@ class dataframe(UIElement[dict[str, Any], DataFrameType]):
def _convert_value(self, value: dict[str, Any]) -> DataFrameType:
if value is None:
self._error = None
- return _maybe_collect(self._data, self._was_lazy)
+ # Return the original data using the undo callback
+ return self._undo(self._transform_container._original_df)
try:
transformations = parse_raw(value, Transformations)
result = self._transform_container.apply(transformations)
self._error = None
self._last_transforms = transformations
- return _maybe_collect(result, self._was_lazy)
+ return self._undo(result)
except Exception as e:
error = f"Error applying dataframe transform: {str(e)}\n\n"
sys.stderr.write(error)
self._error = error
- return _maybe_collect(
- nw.from_native(self._data, pass_through=False).lazy(),
- self._was_lazy,
- )
+ return self._undo(self._transform_container._original_df)
def _search(self, args: SearchTableArgs) -> SearchTableResponse:
offset = args.page_number * args.page_size
@@ -341,11 +331,3 @@ class dataframe(UIElement[dict[str, Any], DataFrameType]):
if limit is not None:
tm = tm.take(limit, 0)
return tm
-
-
-def _maybe_collect(
- df: nw.LazyFrame[IntoLazyFrame], was_lazy: bool
-) -> DataFrameType:
- if was_lazy:
- return df.collect().to_native() # type: ignore[no-any-return]
- return df.to_native()
diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/apply.py b/marimo/_plugins/ui/_impl/dataframes/transforms/apply.py
index edb15cc75b..e9958dedfa 100644
--- a/marimo/_plugins/ui/_impl/dataframes/transforms/apply.py
+++ b/marimo/_plugins/ui/_impl/dataframes/transforms/apply.py
@@ -14,7 +14,7 @@ from marimo._plugins.ui._impl.dataframes.transforms.types import (
TransformType,
)
from marimo._utils.assert_never import assert_never
-from marimo._utils.narwhals_utils import can_narwhalify, is_narwhals_lazyframe
+from marimo._utils.narwhals_utils import can_narwhalify, make_lazy
T = TypeVar("T")
@@ -61,22 +61,15 @@ def apply_transforms_to_df(
f"Unsupported dataframe type. Must be Pandas, Polars, Ibis, Pyarrow, or DuckDB. Got: {type(df)}"
)
- import narwhals.stable.v2 as nw
-
- nw_df = nw.from_native(df)
- was_lazy = is_narwhals_lazyframe(nw_df)
- nw_df = nw_df.lazy()
+ lazy_df, undo = make_lazy(df)
result_nw = _apply_transforms(
- nw_df,
+ lazy_df,
NarwhalsTransformHandler(),
Transformations(transforms=[transform]),
)
- if was_lazy:
- return result_nw.to_native()
-
- return result_nw.collect().to_native() # type: ignore[no-any-return]
+ return undo(result_nw)
def _apply_transforms(
diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py b/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
index debfd56231..0a25dbf4bf 100644
--- a/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
+++ b/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
@@ -31,6 +31,7 @@ from marimo._plugins.ui._impl.dataframes.transforms.types import (
UniqueTransform,
)
from marimo._utils.assert_never import assert_never
+from marimo._utils.narwhals_utils import collect_and_preserve_type
if TYPE_CHECKING:
import polars as pl
@@ -293,20 +294,22 @@ class NarwhalsTransformHandler(TransformHandler[DataFrame]):
df: DataFrame, transform: ShuffleRowsTransform
) -> DataFrame:
# Note: narwhals sample requires collecting first for shuffle with seed
- result = df.collect().sample(fraction=1, seed=transform.seed)
- return result.lazy()
+ collected_df, undo = collect_and_preserve_type(df)
+ result = collected_df.sample(fraction=1, seed=transform.seed)
+ return undo(result)
@staticmethod
def handle_sample_rows(
df: DataFrame, transform: SampleRowsTransform
) -> DataFrame:
# Note: narwhals sample requires collecting first for shuffle with seed
- result = df.collect().sample(
+ collected_df, undo = collect_and_preserve_type(df)
+ result = collected_df.sample(
n=transform.n,
seed=transform.seed,
with_replacement=transform.replace,
)
- return result.lazy()
+ return undo(result)
@staticmethod
def handle_explode_columns(
diff --git a/marimo/_utils/narwhals_utils.py b/marimo/_utils/narwhals_utils.py
index a8b9cdfedb..9bdb62d852 100644
--- a/marimo/_utils/narwhals_utils.py
+++ b/marimo/_utils/narwhals_utils.py
@@ -2,7 +2,7 @@
from __future__ import annotations
import sys
-from typing import TYPE_CHECKING, Any, Union, overload
+from typing import TYPE_CHECKING, Any, Callable, Union, overload
import narwhals as nw_main
import narwhals.dtypes as nw_dtypes
@@ -10,6 +10,10 @@ import narwhals.stable.v1 as nw1
import narwhals.stable.v2 as nw
from narwhals.typing import IntoDataFrame
+from marimo import _loggers
+
+LOGGER = _loggers.marimo_logger()
+
if sys.version_info < (3, 11):
from typing_extensions import TypeGuard
else:
@@ -17,7 +21,12 @@ else:
if TYPE_CHECKING:
- from narwhals.typing import IntoDataFrame, IntoFrame, IntoLazyFrame
+ from narwhals.typing import (
+ IntoBackend,
+ IntoDataFrame,
+ IntoFrame,
+ IntoLazyFrame,
+ )
from typing_extensions import TypeIs
@@ -239,3 +248,109 @@ def is_narwhals_dataframe(df: Any) -> TypeIs[nw.DataFrame[Any]]:
or isinstance(df, nw_main.DataFrame)
or isinstance(df, nw1.DataFrame)
)
+
+
+if TYPE_CHECKING:
+ UndoCallback = Callable[
+ [Union[nw.LazyFrame[Any], nw.DataFrame[Any]]], IntoFrame
+ ]
+
+
+def _to_lazyframe(
+ df: Union[nw.DataFrame[Any], nw.LazyFrame[Any]],
+ original_backend: IntoBackend[Any],
+) -> nw.LazyFrame[Any]:
+ if is_narwhals_lazyframe(df):
+ return df
+ else:
+ try:
+ # Try to convert to the original backend. This backend must be a "lazy backend"
+ # e.g., Ibis, DuckDB, etc.
+ return df.lazy(backend=original_backend)
+ except ValueError:
+ # This error is expected in most cases. For example, if the original
+ # backend was not a "lazy backend" (e.g., Pandas), Narwhals will
+ # raise a ValueError. In this case, we just make a default lazyframe.
+ return df.lazy()
+
+
+def _to_dataframe(
+ df: Union[nw.DataFrame[Any], nw.LazyFrame[Any]],
+) -> nw.DataFrame[Any]:
+ if is_narwhals_dataframe(df):
+ return df
+ else:
+ return df.collect()
+
+
+def make_lazy(
+ df: IntoFrame,
+) -> tuple[nw.LazyFrame[Any], UndoCallback]:
+ """
+ Convert a dataframe to a lazy narwhals LazyFrame and return an undo callback.
+
+ This utility tracks whether the original dataframe was lazy or eager,
+ and provides a callback to convert back to the original type.
+
+ Args:
+ df: A dataframe that can be narwhalified (Pandas, Polars, Ibis, etc.)
+
+ Returns:
+ A tuple of:
+ - nw.LazyFrame: The lazy version of the dataframe
+ - undo: A callback that takes a LazyFrame and converts it back to the
+ original type (lazy or eager), returning the native dataframe
+
+ Example:
+ >>> lazy_df, undo = make_lazy(ibis_table)
+ >>> # Do transformations on lazy_df
+ >>> result = undo(lazy_df) # Returns Ibis table (still lazy)
+ """
+ nw_df = nw.from_native(df, pass_through=False)
+ was_lazy = is_narwhals_lazyframe(nw_df)
+ original_backend = nw_df.implementation
+ lazy_df = nw_df.lazy()
+
+ def undo(result: Union[nw.LazyFrame[Any], nw.DataFrame[Any]]) -> Any:
+ """Convert back to the original type (lazy or eager)."""
+ if not is_narwhals_dataframe(result) and not is_narwhals_lazyframe(
+ result
+ ):
+ LOGGER.warning(
+ "Expected a narwhals DataFrame or LazyFrame, got %s",
+ type(result),
+ )
+ return result
+
+ if was_lazy:
+ return _to_lazyframe(result, original_backend).to_native()
+ else:
+ return _to_dataframe(result).to_native()
+
+ return lazy_df, undo
+
+
+def collect_and_preserve_type(
+ df: nw.LazyFrame[Any],
+) -> tuple[
+ nw.DataFrame[Any], Callable[[nw.DataFrame[Any]], nw.LazyFrame[Any]]
+]:
+ """
+ Collect a narwhals LazyFrame to DataFrame, preserving the original backend.
+
+ This is useful since when you collect an Ibis or DuckDB dataframe, making them
+ lazy does not convert them back to their original backend.
+ """
+ original_backend = df.implementation
+
+ def undo(result: nw.DataFrame[Any]) -> nw.LazyFrame[Any]:
+ """Convert back to the original backend as a LazyFrame."""
+ if not is_narwhals_dataframe(result):
+ LOGGER.warning(
+ "Expected a narwhals DataFrame, got %s", type(result)
+ )
+ return result.lazy()
+
+ return _to_lazyframe(result, original_backend)
+
+ return df.collect(), undo
diff --git a/tests/_plugins/ui/_impl/charts/test_altair_transformers.py b/tests/_plugins/ui/_impl/charts/test_altair_transformers.py
index d3bfbcc466..480d7f7175 100644
--- a/tests/_plugins/ui/_impl/charts/test_altair_transformers.py
+++ b/tests/_plugins/ui/_impl/charts/test_altair_transformers.py
@@ -17,6 +17,7 @@ from marimo._plugins.ui._impl.charts.altair_transformer import (
_to_marimo_inline_csv,
_to_marimo_json,
register_transformers,
+ sanitize_nan_infs,
)
from tests._data.mocks import DFType, create_dataframes
@@ -288,3 +289,21 @@ def test_to_marimo_arrow_different_dtypes(df: IntoDataFrame):
assert "url" in result
assert "format" in result
assert result["format"] == {"type": "arrow"}
+
+
+ "df",
+ create_dataframes(
+ {
+ "A": [1.0, float("nan"), 3.0],
+ "B": [float("inf"), 2.0, float("-inf")],
+ },
+ ),
+)
+def test_sanitize_nan_infs_preserves_df_type(df: IntoDataFrame):
+ """Test that sanitize_nan_infs preserves eager dataframe type."""
+ original_type = type(df)
+
+ result = sanitize_nan_infs(df)
+
+ assert type(result) is original_type
diff --git a/tests/_plugins/ui/_impl/dataframes/test_dataframe.py b/tests/_plugins/ui/_impl/dataframes/test_dataframe.py
index 34aea85cb7..3adcb7d38b 100644
--- a/tests/_plugins/ui/_impl/dataframes/test_dataframe.py
+++ b/tests/_plugins/ui/_impl/dataframes/test_dataframe.py
@@ -102,6 +102,8 @@ class TestDataframes:
with pytest.raises(ColumnNotFound):
subject._get_column_values(GetColumnValuesArgs(column="idk"))
+ assert type(subject.value) is type(df)
+
@staticmethod
@pytest.mark.parametrize(
"df",
@@ -131,6 +133,8 @@ class TestDataframes:
with pytest.raises(ColumnNotFound):
subject._get_column_values(GetColumnValuesArgs(column=1))
+ assert type(subject.value) is type(df)
+
@staticmethod
@pytest.mark.skipif(
is_windows(), reason="windows produces different csv output"
@@ -167,6 +171,7 @@ class TestDataframes:
)
assert search_result.total_rows == 3
assert search_result.data == result.url
+ assert type(subject.value) is type(df)
@staticmethod
@pytest.mark.parametrize(
@@ -210,6 +215,8 @@ class TestDataframes:
assert response.too_many_values is True
assert len(response.values) == 0
+ assert type(subject.value) is type(df)
+
@staticmethod
@pytest.mark.parametrize(
"df",
@@ -229,6 +236,7 @@ class TestDataframes:
)
assert search_result.total_rows == 100
assert search_result.data == result.url
+ assert type(subject.value) is type(df)
@staticmethod
def test_dataframe_too_large_page_size() -> None:
@@ -256,6 +264,7 @@ class TestDataframes:
) == GetColumnValuesResponse(
values=["a", "b", "c"], too_many_values=False
)
+ assert type(subject.value) is type(df)
@staticmethod
@pytest.mark.parametrize(
@@ -275,6 +284,7 @@ class TestDataframes:
SearchTableArgs(page_size=10, page_number=0)
)
assert search_result.total_rows == 100
+ assert type(subject.value) is type(df)
@staticmethod
@pytest.mark.skipif(
@@ -293,6 +303,7 @@ class TestDataframes:
# show_download=False
subject = ui.dataframe(df, show_download=False)
assert subject._component_args["show-download"] is False
+ assert type(subject.value) is type(df)
@staticmethod
@pytest.mark.skipif(
@@ -314,6 +325,7 @@ class TestDataframes:
data_bytes = from_data_uri(download_url)[1]
assert len(data_bytes) > 0
+ assert type(subject.value) is type(df)
@staticmethod
@pytest.mark.skipif(
@@ -343,6 +355,7 @@ class TestDataframes:
assert "Bob" in names
assert "Charlie" in names
assert "Alice" not in names
+ assert type(subject.value) is type(df)
@staticmethod
@pytest.mark.skipif(
@@ -373,6 +386,7 @@ class TestDataframes:
assert "format must be one of 'csv', 'json', or 'parquet'" in str(
exc_info.value
)
+ assert type(subject.value) is type(df)
@staticmethod
@pytest.mark.skipif(
@@ -399,6 +413,8 @@ class TestDataframes:
# Some backends might not support all formats
pytest.skip(f"Backend doesn't support {format_type}: {e}")
+ assert type(subject.value) is type(df)
+
@staticmethod
@pytest.mark.parametrize(
"df",
@@ -414,6 +430,8 @@ class TestDataframes:
with pytest.raises(ColumnNotFound):
subject._get_column_values(GetColumnValuesArgs(column="C"))
+ assert type(subject.value) is type(df)
+
@staticmethod
@pytest.mark.skipif(not HAS_POLARS, reason="Polars not installed")
def test_polars_groupby_alias() -> None:
@@ -602,6 +620,7 @@ def test_ibis_with_polars_backend() -> None:
assert dataframe._get_dataframe(EmptyArgs()).total_rows == 3
assert dataframe._get_dataframe(EmptyArgs()).sql_code is None
ibis.set_backend(prev_backend)
+ assert type(dataframe.value) is type(memtable)
@pytest.mark.skipif(
@@ -620,6 +639,22 @@ def test_dataframe_with_int_column_names():
assert "DataFrame has integer column names" in str(w[0].message)
assert dataframe.value is not None
+ assert type(dataframe.value) is type(data)
+
+
+ "df",
+ create_dataframes(
+ {"A": [1, 2, 3], "B": ["a", "b", "c"]},
+ ),
+)
+def test_dataframe_types_are_preserved(df: IntoDataFrame):
+ """Test that dataframe types are preserved after using mo.ui.dataframe."""
+ ui_limit = 3
+ # Create a marimo UI dataframe with a preview limit
+ ui_df = ui.dataframe(df, limit=ui_limit)
+
+ assert type(df) is type(ui_df.value)
@pytest.mark.skipif(
@@ -658,3 +693,4 @@ def test_base_exception_handling():
# Verify the error message is preserved
assert "to json panic" in str(exc_info.value)
assert exc_info.value.error == str(exc_info.value)
+ assert type(table.value) is type(df)
diff --git a/tests/_plugins/ui/_impl/dataframes/test_handlers.py b/tests/_plugins/ui/_impl/dataframes/test_handlers.py
index 852abf047b..6a57950259 100644
--- a/tests/_plugins/ui/_impl/dataframes/test_handlers.py
+++ b/tests/_plugins/ui/_impl/dataframes/test_handlers.py
@@ -33,7 +33,7 @@ from marimo._plugins.ui._impl.dataframes.transforms.types import (
TransformType,
UniqueTransform,
)
-from marimo._utils.narwhals_utils import is_narwhals_lazyframe
+from marimo._utils.narwhals_utils import is_narwhals_lazyframe, make_lazy
from tests._data.mocks import create_dataframes
pytest.importorskip("ibis")
@@ -72,6 +72,7 @@ def collect_df(df: DataFrameType) -> nw.DataFrame[Any]:
def assert_frame_equal(a: DataFrameType, b: DataFrameType) -> None:
nw_a = collect_df(a)
nw_b = collect_df(b)
+ assert type(a) is type(b)
assert nw_a.to_dict(as_series=False) == nw_b.to_dict(as_series=False)
@@ -896,6 +897,7 @@ class TestTransformHandler:
nw_result = collect_df(result)
assert "A" in nw_result.columns
assert "B" in nw_result.columns
+ assert type(result) is type(expected)
@staticmethod
@pytest.mark.parametrize(
@@ -911,6 +913,7 @@ class TestTransformHandler:
nw_result = collect_df(result)
assert "A" in nw_result.columns
assert "B" in nw_result.columns
+ assert type(result) is type(df)
@staticmethod
@pytest.mark.parametrize(
@@ -1036,6 +1039,7 @@ class TestTransformHandler:
nw_expected.sort("A"),
nw_result.sort("A"),
)
+ assert type(result) is type(df)
@staticmethod
@pytest.mark.parametrize(
@@ -1051,7 +1055,7 @@ class TestTransformHandler:
def test_transforms_container(
df: DataFrameType, expected: DataFrameType, expected2: DataFrameType
) -> None:
- nw_df = nw.from_native(df).lazy()
+ nw_df, undo = make_lazy(df)
container = TransformsContainer(nw_df, NarwhalsTransformHandler())
# Define some transformations
@@ -1079,7 +1083,7 @@ class TestTransformHandler:
# Get the transformed dataframe
# Check that the transformations were applied correctly
- assert_frame_equal(result, expected)
+ assert_frame_equal(undo(result), expected)
# Reapply transforms by adding a new one
filter_again_transform = FilterRowsTransform(
@@ -1099,7 +1103,7 @@ class TestTransformHandler:
transformations,
)
# Check that the transformations were applied correctly
- assert_frame_equal(result, expected2)
+ assert_frame_equal(undo(result), expected2)
transformations = Transformations([sort_transform, filter_transform])
# Verify the next transformation
@@ -1113,7 +1117,7 @@ class TestTransformHandler:
transformations,
)
# Check that the transformations were applied correctly
- assert_frame_equal(result, expected)
+ assert_frame_equal(undo(result), expected)
@staticmethod
@pytest.mark.parametrize(
diff --git a/tests/_plugins/ui/_impl/test_altair_chart.py b/tests/_plugins/ui/_impl/test_altair_chart.py
index 386f2431c2..26430390c8 100644
--- a/tests/_plugins/ui/_impl/test_altair_chart.py
+++ b/tests/_plugins/ui/_impl/test_altair_chart.py
@@ -1834,3 +1834,25 @@ def test_using_vegafusion() -> None:
# Clean up
alt.data_transformers.enable("default")
+
+
+ "df",
+ create_dataframes(
+ {
+ "field": ["value1", "value2", "value3", "value4"],
+ "color": ["red", "red", "blue", "blue"],
+ "field_2": [1, 2, 3, 4],
+ },
+ ),
+)
+def test_filter_dataframe_preserves_df_type(df: ChartDataType) -> None:
+ """Test that _filter_dataframe preserves dataframe type."""
+ original_type = type(df)
+
+ # Filter with an interval selection
+ selection: ChartSelection = {"signal": {"field_2": [1, 3]}}
+ result = _filter_dataframe(df, selection=selection)
+
+ # Type should be preserved
+ assert type(result) is original_type
diff --git a/tests/_plugins/ui/_impl/test_table.py b/tests/_plugins/ui/_impl/test_table.py
index 408feacc55..5902b603cc 100644
--- a/tests/_plugins/ui/_impl/test_table.py
+++ b/tests/_plugins/ui/_impl/test_table.py
@@ -250,6 +250,7 @@ def test_value() -> None:
data = ["banana", "apple", "cherry", "date", "elderberry"]
table = ui.table(data)
assert list(table.value) == []
+ assert type(table.value) is type(data)
def test_value_with_selection() -> None:
@@ -259,12 +260,14 @@ def test_value_with_selection() -> None:
"banana",
"cherry",
]
+ assert type(table.value) is type(data)
def test_value_with_initial_selection() -> None:
data = ["banana", "apple", "cherry", "date", "elderberry"]
table = ui.table(data, initial_selection=[0, 2])
assert table.value == ["banana", "cherry"]
+ assert type(table.value) is type(data)
def test_value_does_not_include_index_column() -> None:
@@ -286,6 +289,7 @@ def test_value_does_not_include_index_column() -> None:
{"name": "Alice", "age": 30},
{"name": "Charlie", "age": 35},
]
+ assert type(table.value) is type(data)
def test_invalid_initial_selection() -> None:
@@ -326,6 +330,7 @@ def test_value_with_sorting_then_selection() -> None:
assert list(table._convert_value(["0"])) == [
{"value": "apple"},
]
+ assert type(table.value) is type(data)
@pytest.mark.parametrize(
@@ -361,6 +366,7 @@ def test_value_with_sorting_then_selection_dfs(df: Any) -> None:
assert not isinstance(value, nw.DataFrame)
assert INDEX_COLUMN_NAME not in value.columns
assert nw.from_native(value)["a"][0] == "x"
+ assert type(table.value) is type(df)
def test_value_with_search_then_selection() -> None:
@@ -401,6 +407,7 @@ def test_value_with_search_then_selection() -> None:
)
)
assert list(table._convert_value(["2"])) == ["cherry"]
+ assert type(table.value) is type(data)
@pytest.mark.parametrize(
@@ -449,6 +456,7 @@ def test_value_with_search_then_selection_dfs(df: Any) -> None:
value = table._convert_value(["2"])
assert not isinstance(value, nw.DataFrame)
assert nw.from_native(value)["a"][0] == "baz"
+ assert type(table.value) is type(df)
@pytest.mark.parametrize(
@@ -536,6 +544,7 @@ def test_value_with_selection_then_sorting_dict_of_lists() -> None:
"Company B",
"Company E",
]
+ assert type(table.value) is type(data)
def test_value_with_cell_selection_then_sorting_dict_of_lists() -> None:
@@ -584,6 +593,7 @@ def test_value_with_cell_selection_then_sorting_dict_of_lists() -> None:
TableCell(row="0", column="company", value="Company B"),
TableCell(row="2", column="company", value="Company E"),
]
+ assert type(table.value) is list
def test_search_sort_nonexistent_columns() -> None:
@@ -600,6 +610,7 @@ def test_search_sort_nonexistent_columns() -> None:
)
assert table._convert_value(["0"]) == ["banana"]
+ assert type(table.value) is type(data)
def test_invalid_index_in_initial_selection() -> None:
@@ -807,6 +818,7 @@ def test_can_get_second_page_with_search_df(df: Any) -> None:
assert len(result_data) == 5
assert int(result_data[0]["a"]) == 23
assert int(result_data[-1]["a"]) == 27
+ assert type(table.value) is type(df)
def test_with_no_pagination() -> None:
@@ -816,6 +828,7 @@ def test_with_no_pagination() -> None:
assert table._component_args["page-size"] == 20
assert table._component_args["total-rows"] == 20
assert len(json.loads(table._component_args["data"])) == 20
+ assert type(table.value) is type(data)
def test_table_with_too_many_rows_and_custom_total() -> None:
@@ -1806,6 +1819,7 @@ def test_json_multi_col_idx_table() -> None:
"('basic_amt', 'QLD')": 2,
}
]
+ assert type(table.value) is type(df)
LAZY_DATAFRAMES = ["lazy-polars", "duckdb", "ibis"]
diff --git a/tests/_utils/test_narwhals_utils.py b/tests/_utils/test_narwhals_utils.py
index 13e6383c35..9ad842b3ae 100644
--- a/tests/_utils/test_narwhals_utils.py
+++ b/tests/_utils/test_narwhals_utils.py
@@ -11,12 +11,14 @@ from marimo._utils.narwhals_utils import (
assert_narwhals_series,
can_narwhalify,
can_narwhalify_lazyframe,
+ collect_and_preserve_type,
dataframe_to_csv,
empty_df,
is_narwhals_integer_type,
is_narwhals_lazyframe,
is_narwhals_string_type,
is_narwhals_temporal_type,
+ make_lazy,
unwrap_narwhals_dataframe,
unwrap_py_scalar,
upgrade_narwhals_df,
@@ -172,3 +174,95 @@ def test_can_narwhalify_lazyframe():
con = duckdb.connect(":memory:")
rel = con.sql("SELECT 1")
assert can_narwhalify_lazyframe(rel) is True
+
+
+ "df",
+ create_dataframes(
+ {"a": [1, 2, 3], "b": ["x", "y", "z"]},
+ ),
+)
+def test_make_lazy_with_all_dataframes(df: IntoDataFrame) -> None:
+ """Test make_lazy() with eager dataframes - should collect back to eager."""
+ original_type = type(df)
+ original_namespace = nw.get_native_namespace(df)
+
+ lazy_df, undo = make_lazy(df)
+ assert is_narwhals_lazyframe(lazy_df)
+
+ # Call undo to convert back
+ result = undo(lazy_df)
+
+ # The result should be the same type as the original
+ assert type(result) is original_type
+ assert nw.get_native_namespace(result) == original_namespace
+
+ # Verify data is preserved
+ result_nw = nw.from_native(result)
+ if is_narwhals_lazyframe(result_nw):
+ result_nw = result_nw.collect()
+
+ assert result_nw.shape == (3, 2)
+ assert set(result_nw.columns) == {"a", "b"}
+
+ # Call undo on a collected DataFrame
+ collected_df = lazy_df.collect()
+ result_from_collected = undo(collected_df)
+ assert type(result_from_collected) is type(df)
+
+
[email protected](not HAS_DEPS, reason="optional dependencies not installed")
+def test_undo_preserves_lazy_or_eager_type():
+ """Test that the undo callback returns the correct type (eager or lazy) matching the original input."""
+ import polars as pl
+
+ df = pl.DataFrame({"a": [1, 2, 3]})
+ lazy_df = pl.DataFrame({"a": [1, 2, 3]}).lazy()
+
+ _, undo = make_lazy(df)
+ _, undo_lazy = make_lazy(lazy_df)
+
+ assert type(undo(nw.from_native(df))) is pl.DataFrame
+ assert type(undo(nw.from_native(lazy_df))) is pl.DataFrame
+
+ assert type(undo_lazy(nw.from_native(df))) is pl.LazyFrame
+ assert type(undo_lazy(nw.from_native(lazy_df))) is pl.LazyFrame
+
+
+ "df",
+ create_dataframes(
+ {"a": [1, 2, 3], "b": ["x", "y", "z"]},
+ ),
+)
+def test_collect_and_preserve_type_dataframes(
+ df: IntoDataFrame,
+) -> None:
+ # Get the original backend
+ original_namespace = nw.get_native_namespace(df)
+
+ # Convert to narwhals LazyFrame
+ lazy_nw = nw.from_native(df).lazy()
+ assert is_narwhals_lazyframe(lazy_nw)
+
+ # Collect and get undo function
+ collected_df, undo = collect_and_preserve_type(lazy_nw)
+
+ # The collected result should be a narwhals DataFrame
+ assert not is_narwhals_lazyframe(collected_df)
+ assert collected_df.shape == (3, 2)
+
+ # Call undo to convert back to LazyFrame
+ result = undo(collected_df)
+
+ # The result should be a LazyFrame
+ assert is_narwhals_lazyframe(result)
+
+ # The result should have the same backend as the original
+ result_namespace = nw.get_native_namespace(result.to_native())
+ assert result_namespace == original_namespace
+
+ # Verify data is preserved
+ result_collected = result.collect()
+ assert result_collected.shape == (3, 2)
+ assert set(result_collected.columns) == {"a", "b"}