Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion marimo/_plugins/ui/_impl/dataframes/transforms/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,15 @@ class SortColumnTransform:
class FilterRowsTransform:
type: Literal[TransformType.FILTER_ROWS]
operation: Literal["keep_rows", "remove_rows"]
where: FilterGroup
where: FilterGroup | list[FilterCondition | FilterGroup]

def __post_init__(self) -> None:
if isinstance(self.where, list):
object.__setattr__(
self,
"where",
FilterGroup(children=tuple(self.where)),
)


@dataclass
Expand Down
31 changes: 31 additions & 0 deletions tests/_plugins/ui/_impl/dataframes/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@
HAS_IBIS = DependencyManager.ibis.has()
HAS_POLARS = DependencyManager.polars.has()

ROW_FILTER_LIST_WHERE_VALUE = {
"transforms": [
{
"type": "filter_rows",
"operation": "keep_rows",
"where": [
{
"type": "condition",
"column_id": "sepal_length",
"operator": ">",
"value": 5,
},
],
},
]
}

if TYPE_CHECKING:
from narwhals.stable.v2.typing import IntoDataFrame, IntoLazyFrame

Expand Down Expand Up @@ -72,6 +89,20 @@ def test_dataframe_supports_dataframe_backends(df: Any) -> None:
# original native type for each supported dataframe backend.
assert type(subject.value) is type(df)

@staticmethod
def test_dataframe_form_accepts_filter_rows_list_where() -> None:
df = pd.DataFrame(
{
"sepal_length": [4.9, 5.1, 6.2],
"species": ["setosa", "setosa", "virginica"],
}
)
subject = ui.dataframe(df).form()

subject._convert_value(ROW_FILTER_LIST_WHERE_VALUE)

assert subject.element._error is None

@staticmethod
@pytest.mark.parametrize(
"df",
Expand Down
22 changes: 22 additions & 0 deletions tests/_plugins/ui/_impl/dataframes/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ def test_parse_transforms() -> None:
assert isinstance(result, Transformations)


def test_parse_filter_rows_transform_with_legacy_list_where() -> None:
value = {
"transforms": [
{
"type": "filter_rows",
"operation": "keep_rows",
"where": [
{
"type": "condition",
"column_id": "sepal_length",
"operator": ">",
"value": 5,
},
],
},
]
}

result = parse_raw(value, Transformations)
assert isinstance(result, Transformations)


def test_parse_transforms_with_in_operator() -> None:
def create_transform(operator: str):
return {
Expand Down
Loading