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.

dataframe plugin - move columns logic to the backend (#7611)

Shahmir Varqhad9d3ce6cf3f4
diff --git a/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx b/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx
index 8487173afe..d36052b5f6 100644
--- a/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx
+++ b/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx
@@ -60,6 +60,7 @@ type PluginFunctions = {
total_rows: number;
row_headers: FieldTypesWithExternalType;
field_types: FieldTypesWithExternalType | null;
+ column_types_per_step: FieldTypesWithExternalType[];
python_code?: string | null;
sql_code?: string | null;
}>;
@@ -112,6 +113,7 @@ export const DataFramePlugin = createPlugin<S>("marimo-dataframe")
total_rows: z.number(),
row_headers: columnToFieldTypesSchema,
field_types: columnToFieldTypesSchema,
+ column_types_per_step: z.array(columnToFieldTypesSchema),
python_code: z.string().nullish(),
sql_code: z.string().nullish(),
}),
@@ -190,8 +192,15 @@ export const DataFrameComponent = memo(
[value?.transforms],
);
- const { url, total_rows, row_headers, field_types, python_code, sql_code } =
- data || {};
+ const {
+ url,
+ total_rows,
+ row_headers,
+ field_types,
+ column_types_per_step,
+ python_code,
+ sql_code,
+ } = data || {};
const [internalValue, setInternalValue] = useState<Transformations>(
value || EMPTY,
@@ -270,6 +279,7 @@ export const DataFrameComponent = memo(
}}
onInvalidChange={setInternalValue}
getColumnValues={get_column_values}
+ columnTypesPerStep={column_types_per_step}
lazy={lazy}
/>
</TabsContent>
diff --git a/frontend/src/plugins/impl/data-frames/panel.tsx b/frontend/src/plugins/impl/data-frames/panel.tsx
index 29b30d9606..d27101d4a1 100644
--- a/frontend/src/plugins/impl/data-frames/panel.tsx
+++ b/frontend/src/plugins/impl/data-frames/panel.tsx
@@ -28,6 +28,7 @@ import React, {
import { useFieldArray, useForm } from "react-hook-form";
import useEvent from "react-use-event-hook";
import type { z } from "zod";
+import type { FieldTypesWithExternalType } from "@/components/data-table/types";
import {
ColumnFetchValuesContext,
ColumnInfoContext,
@@ -58,7 +59,7 @@ import {
TransformTypeSchema,
} from "./schema";
import type { ColumnDataTypes } from "./types";
-import { getUpdatedColumnTypes } from "./utils/getUpdatedColumnTypes";
+import { getEffectiveColumns } from "./utils/getEffectiveColumns";
export interface TransformPanelHandle {
submit: () => void;
@@ -73,6 +74,8 @@ interface Props {
values: unknown[];
too_many_values: boolean;
}>;
+ // Column types at each transform step (index 0 = original, index N = after N transforms)
+ columnTypesPerStep?: FieldTypesWithExternalType[];
lazy: boolean;
ref?: React.Ref<TransformPanelHandle>;
}
@@ -83,6 +86,7 @@ export const TransformPanel: React.FC<Props> = ({
onChange,
onInvalidChange,
getColumnValues,
+ columnTypesPerStep,
lazy,
ref,
}) => {
@@ -157,8 +161,7 @@ export const TransformPanel: React.FC<Props> = ({
});
const effectiveColumns = useMemo(() => {
- const transformsBeforeSelected = transforms.slice(0, selectedTransform);
- return getUpdatedColumnTypes(transformsBeforeSelected, columns);
+ return getEffectiveColumns(columns, columnTypesPerStep, selectedTransform);
}, [columns, transforms, selectedTransform]);
const handleAddTransform = (transform: z.ZodType) => {
diff --git a/frontend/src/plugins/impl/data-frames/utils/__tests__/getEffectiveColumns.test.ts b/frontend/src/plugins/impl/data-frames/utils/__tests__/getEffectiveColumns.test.ts
new file mode 100644
index 0000000000..9f37c1ddcf
--- /dev/null
+++ b/frontend/src/plugins/impl/data-frames/utils/__tests__/getEffectiveColumns.test.ts
@@ -0,0 +1,134 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { describe, expect, it } from "vitest";
+import type { FieldTypesWithExternalType } from "@/components/data-table/types";
+import type { ColumnDataTypes, ColumnId } from "../../types";
+import { getEffectiveColumns } from "../getEffectiveColumns";
+
+describe("getEffectiveColumns", () => {
+ const originalColumns: ColumnDataTypes = new Map([
+ ["id" as ColumnId, "int64"],
+ ["name" as ColumnId, "string"],
+ ["value" as ColumnId, "float64"],
+ ]);
+
+ // Helper to create field types in the backend format
+ const createFieldTypes = (
+ cols: [string, string][],
+ ): FieldTypesWithExternalType => {
+ return cols.map(([name, dataType]) => [
+ name,
+ [dataType, dataType],
+ ]) as FieldTypesWithExternalType;
+ };
+
+ it("returns original columns when columnTypesPerStep is undefined", () => {
+ const result = getEffectiveColumns(originalColumns, undefined, 0);
+ expect(result).toBe(originalColumns);
+ });
+
+ it("returns original columns when columnTypesPerStep is empty", () => {
+ const result = getEffectiveColumns(originalColumns, [], 0);
+ expect(result).toBe(originalColumns);
+ });
+
+ it("returns original columns when selectedTransform is undefined", () => {
+ const columnTypesPerStep = [
+ createFieldTypes([
+ ["id", "int64"],
+ ["name", "string"],
+ ]),
+ ];
+ const result = getEffectiveColumns(
+ originalColumns,
+ columnTypesPerStep,
+ undefined,
+ );
+ // Should use index 0 (original columns from backend)
+ expect(result.size).toBe(2);
+ expect(result.get("id" as ColumnId)).toBe("int64");
+ expect(result.get("name" as ColumnId)).toBe("string");
+ });
+
+ it("returns columns at the correct step index", () => {
+ const columnTypesPerStep = [
+ // Step 0: original
+ createFieldTypes([
+ ["id", "int64"],
+ ["name", "string"],
+ ["value", "float64"],
+ ]),
+ // Step 1: after first transform (renamed 'name' to 'title')
+ createFieldTypes([
+ ["id", "int64"],
+ ["title", "string"],
+ ["value", "float64"],
+ ]),
+ // Step 2: after second transform (selected only id and title)
+ createFieldTypes([
+ ["id", "int64"],
+ ["title", "string"],
+ ]),
+ ];
+
+ // When editing transform 0, should see original columns (step 0)
+ const result0 = getEffectiveColumns(originalColumns, columnTypesPerStep, 0);
+ expect(result0.size).toBe(3);
+ expect(result0.has("name" as ColumnId)).toBe(true);
+
+ // When editing transform 1, should see columns after transform 0 (step 1)
+ const result1 = getEffectiveColumns(originalColumns, columnTypesPerStep, 1);
+ expect(result1.size).toBe(3);
+ expect(result1.has("title" as ColumnId)).toBe(true);
+ expect(result1.has("name" as ColumnId)).toBe(false);
+
+ // When editing transform 2, should see columns after transform 1 (step 2)
+ const result2 = getEffectiveColumns(originalColumns, columnTypesPerStep, 2);
+ expect(result2.size).toBe(2);
+ expect(result2.has("value" as ColumnId)).toBe(false);
+ });
+
+ it("clamps to max valid index when selectedTransform exceeds array length", () => {
+ const columnTypesPerStep = [
+ createFieldTypes([
+ ["id", "int64"],
+ ["name", "string"],
+ ]),
+ createFieldTypes([
+ ["id", "int64"],
+ ["renamed", "string"],
+ ]),
+ ];
+
+ // selectedTransform = 5, but only 2 steps available
+ // Should clamp to index 1 (last available)
+ const result = getEffectiveColumns(originalColumns, columnTypesPerStep, 5);
+ expect(result.size).toBe(2);
+ expect(result.has("renamed" as ColumnId)).toBe(true);
+ });
+
+ it("handles pivot transform with dynamic column names", () => {
+ const columnTypesPerStep = [
+ // Step 0: original
+ createFieldTypes([
+ ["product", "string"],
+ ["color", "string"],
+ ["sales", "int64"],
+ ]),
+ // Step 1: after pivot on 'color' with values 'red', 'blue'
+ createFieldTypes([
+ ["product", "string"],
+ ["sales_red_sum", "int64"],
+ ["sales_blue_sum", "int64"],
+ ]),
+ ];
+
+ const result = getEffectiveColumns(originalColumns, columnTypesPerStep, 1);
+ expect(result.size).toBe(3);
+ expect(result.has("product" as ColumnId)).toBe(true);
+ expect(result.has("sales_red_sum" as ColumnId)).toBe(true);
+ expect(result.has("sales_blue_sum" as ColumnId)).toBe(true);
+ expect(result.has("color" as ColumnId)).toBe(false);
+ expect(result.has("sales" as ColumnId)).toBe(false);
+ });
+});
diff --git a/frontend/src/plugins/impl/data-frames/utils/__tests__/getUpdatedColumnTypes.test.ts b/frontend/src/plugins/impl/data-frames/utils/__tests__/getUpdatedColumnTypes.test.ts
deleted file mode 100644
index 16f14babcc..0000000000
--- a/frontend/src/plugins/impl/data-frames/utils/__tests__/getUpdatedColumnTypes.test.ts
+++ /dev/null
@@ -1,192 +0,0 @@
-/* Copyright 2026 Marimo. All rights reserved. */
-import { describe, expect, it } from "vitest";
-import type { TransformType } from "../../schema";
-import type { ColumnId } from "../../types";
-import { getUpdatedColumnTypes } from "../getUpdatedColumnTypes";
-
-const INITIAL_COLUMN_TYPES = new Map<ColumnId, string>([
- ["col1" as ColumnId, "str"],
- [2 as ColumnId, "bool"],
- ["col3" as ColumnId, "int"],
-]);
-
-const Transforms = {
- COLUMN_CONVERSION: {
- type: "column_conversion",
- column_id: "col1" as ColumnId,
- data_type: "bool",
- errors: "ignore",
- } satisfies TransformType,
- RENAME_COLUMN: {
- type: "rename_column",
- column_id: 2 as ColumnId,
- new_column_id: "newCol2" as ColumnId,
- } satisfies TransformType,
- GROUP_BY: {
- type: "group_by",
- column_ids: ["col1", "col3"] as ColumnId[],
- aggregation: "max",
- drop_na: true,
- aggregation_column_ids: [],
- } satisfies TransformType,
- GROUP_BY_CHAINED: {
- type: "group_by",
- column_ids: ["newCol2"] as ColumnId[],
- aggregation: "max",
- drop_na: true,
- aggregation_column_ids: [],
- } satisfies TransformType,
- AGGREGATE: {
- type: "aggregate",
- column_ids: ["col1"] as ColumnId[],
- aggregations: ["max"],
- } satisfies TransformType,
- AGGREGATE_CHAINED: {
- type: "aggregate",
- column_ids: ["col1", "col3"] as ColumnId[],
- aggregations: ["max"],
- } satisfies TransformType,
- SELECT_COLUMNS: {
- type: "select_columns",
- column_ids: ["col1", 2] as ColumnId[],
- } satisfies TransformType,
- EXPLODE_COLUMNS: {
- type: "explode_columns",
- column_ids: ["col1", 2] as ColumnId[],
- } satisfies TransformType,
- EXPAND_DICT: {
- type: "expand_dict",
- column_id: "col1" as ColumnId,
- } satisfies TransformType,
- UNIQUE: {
- type: "unique",
- column_ids: ["col1"] as ColumnId[],
- keep: "first",
- } satisfies TransformType,
-};
-
-describe("getUpdatedColumnTypes", () => {
- it("should update column types for column conversion", () => {
- const result = getUpdatedColumnTypes(
- [Transforms.COLUMN_CONVERSION],
- INITIAL_COLUMN_TYPES,
- );
- expect(result).toMatchInlineSnapshot(`
- Map {
- "col1" => "bool",
- 2 => "bool",
- "col3" => "int",
- }
- `);
- });
-
- it("should update column types for rename conversion", () => {
- const result = getUpdatedColumnTypes(
- [Transforms.RENAME_COLUMN],
- INITIAL_COLUMN_TYPES,
- );
- expect(result).toMatchInlineSnapshot(`
- Map {
- "col1" => "str",
- "col3" => "int",
- "newCol2" => "bool",
- }
- `);
- });
-
- it("should update column types for group-by conversion", () => {
- const result = getUpdatedColumnTypes(
- [Transforms.GROUP_BY],
- INITIAL_COLUMN_TYPES,
- );
- expect(result).toMatchInlineSnapshot(`
- Map {
- "col1" => "str",
- "2_max" => "bool",
- "col3" => "int",
- }
- `);
- });
-
- it("should update column types for aggregate conversion", () => {
- const result = getUpdatedColumnTypes(
- [Transforms.AGGREGATE],
- INITIAL_COLUMN_TYPES,
- );
- expect(result).toMatchInlineSnapshot(`
- Map {
- "col1" => "str",
- }
- `);
- });
-
- it("should update column types for select-columns conversion", () => {
- const result = getUpdatedColumnTypes(
- [Transforms.SELECT_COLUMNS],
- INITIAL_COLUMN_TYPES,
- );
- expect(result).toMatchInlineSnapshot(`
- Map {
- "col1" => "str",
- 2 => "bool",
- }
- `);
- });
-
- it("should update column types for explode-columns conversion", () => {
- const result = getUpdatedColumnTypes(
- [Transforms.EXPLODE_COLUMNS],
- INITIAL_COLUMN_TYPES,
- );
- expect(result).toMatchInlineSnapshot(`
- Map {
- "col1" => "str",
- 2 => "bool",
- "col3" => "int",
- }
- `);
- });
-
- it("should update column types for expand-dict conversion", () => {
- const result = getUpdatedColumnTypes(
- [Transforms.EXPAND_DICT],
- INITIAL_COLUMN_TYPES,
- );
- expect(result).toMatchInlineSnapshot(`
- Map {
- "col1" => "str",
- 2 => "bool",
- "col3" => "int",
- }
- `);
- });
-
- it("should update column types for unique conversion", () => {
- const result = getUpdatedColumnTypes(
- [Transforms.UNIQUE],
- INITIAL_COLUMN_TYPES,
- );
- expect(result).toMatchInlineSnapshot(`
- Map {
- "col1" => "str",
- 2 => "bool",
- "col3" => "int",
- }
- `);
- });
-
- it("can chain multiple transforms", () => {
- const result = getUpdatedColumnTypes(
- [
- Transforms.COLUMN_CONVERSION,
- Transforms.RENAME_COLUMN,
- Transforms.GROUP_BY_CHAINED,
- Transforms.AGGREGATE_CHAINED,
- ],
- INITIAL_COLUMN_TYPES,
- );
- expect(result).toMatchInlineSnapshot(`
- Map {}
- `);
- });
-});
diff --git a/frontend/src/plugins/impl/data-frames/utils/getEffectiveColumns.ts b/frontend/src/plugins/impl/data-frames/utils/getEffectiveColumns.ts
new file mode 100644
index 0000000000..607efd1009
--- /dev/null
+++ b/frontend/src/plugins/impl/data-frames/utils/getEffectiveColumns.ts
@@ -0,0 +1,41 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import type { FieldTypesWithExternalType } from "@/components/data-table/types";
+import type { ColumnDataTypes, ColumnId } from "../types";
+
+/**
+ * Get the effective columns at a given transform step.
+ *
+ * @param columns - Original columns (fallback)
+ * @param columnTypesPerStep - Column types at each step from backend
+ * - Index 0 = original columns
+ * - Index N = columns after transform N-1
+ * @param selectedTransform - Index of the currently selected transform
+ * @returns The effective columns before the selected transform
+ */
+export function getEffectiveColumns(
+ columns: ColumnDataTypes,
+ columnTypesPerStep: FieldTypesWithExternalType[] | undefined,
+ stepIndex = 0,
+): ColumnDataTypes {
+ // If no columnTypesPerStep, fall back to original columns
+ if (!columnTypesPerStep || columnTypesPerStep.length === 0) {
+ return columns;
+ }
+
+ // columnTypesPerStep[0] = original columns
+ // columnTypesPerStep[N] = columns after transform N-1
+ // For the selected transform, we want columns BEFORE it, so index = selectedTransform
+ const safeIndex = Math.min(stepIndex, columnTypesPerStep.length - 1);
+ const fieldTypes = columnTypesPerStep[safeIndex];
+
+ if (!fieldTypes) {
+ return columns;
+ }
+
+ const newColumns = new Map<ColumnId, string>();
+ for (const [name, [dataType]] of fieldTypes) {
+ newColumns.set(name as ColumnId, dataType);
+ }
+ return newColumns;
+}
diff --git a/frontend/src/plugins/impl/data-frames/utils/getUpdatedColumnTypes.ts b/frontend/src/plugins/impl/data-frames/utils/getUpdatedColumnTypes.ts
deleted file mode 100644
index 709d99d18b..0000000000
--- a/frontend/src/plugins/impl/data-frames/utils/getUpdatedColumnTypes.ts
+++ /dev/null
@@ -1,88 +0,0 @@
-/* Copyright 2026 Marimo. All rights reserved. */
-import { logNever } from "@/utils/assertNever";
-import { Maps } from "@/utils/maps";
-import type { TransformType } from "../schema";
-import type { ColumnDataTypes, ColumnId } from "../types";
-
-/**
- * Given a list of transforms, return the updated column names/types.
- */
-export function getUpdatedColumnTypes(
- transforms: TransformType[],
- columnTypes: ColumnDataTypes,
-): ColumnDataTypes {
- if (!transforms || transforms.length === 0) {
- return columnTypes;
- }
-
- let next: ColumnDataTypes = new Map(columnTypes);
- for (const transform of transforms) {
- next = handleTransform(transform, next);
- }
-
- return next;
-}
-
-function handleTransform(
- transform: TransformType,
- next: ColumnDataTypes,
-): ColumnDataTypes {
- switch (transform.type) {
- case "column_conversion":
- if (!transform.column_id) {
- return next;
- }
-
- next.set(transform.column_id, transform.data_type);
- return next;
- case "rename_column": {
- if (!transform.new_column_id) {
- return next;
- }
- const type = next.get(transform.column_id);
- if (type) {
- next.set(transform.new_column_id, type);
- next.delete(transform.column_id);
- }
- return next;
- }
- case "group_by": {
- const groupColumns = new Set(transform.column_ids ?? []);
- const aggregationColumns =
- transform.aggregation_column_ids &&
- transform.aggregation_column_ids.length > 0
- ? new Set(transform.aggregation_column_ids)
- : null;
-
- const updated = new Map<ColumnId, string>();
-
- for (const [columnId, type] of next.entries()) {
- if (groupColumns.has(columnId)) {
- updated.set(columnId, type);
- continue;
- }
-
- if (aggregationColumns === null || aggregationColumns.has(columnId)) {
- updated.set(`${columnId}_${transform.aggregation}` as ColumnId, type);
- }
- }
-
- return updated;
- }
- case "aggregate":
- return Maps.filterMap(next, (_v, k) => transform.column_ids.includes(k));
- case "select_columns":
- return Maps.filterMap(next, (_v, k) => transform.column_ids.includes(k));
- case "filter_rows":
- case "shuffle_rows":
- case "sample_rows":
- case "sort_column":
- case "expand_dict":
- case "explode_columns":
- case "unique":
- return next;
- default:
- logNever(transform);
- return next;
- }
-}
diff --git a/marimo/_plugins/ui/_impl/dataframes/dataframe.py b/marimo/_plugins/ui/_impl/dataframes/dataframe.py
index 2bdad5124b..2739150ae2 100644
--- a/marimo/_plugins/ui/_impl/dataframes/dataframe.py
+++ b/marimo/_plugins/ui/_impl/dataframes/dataframe.py
@@ -58,6 +58,8 @@ class GetDataFrameResponse:
# This really only applies to Pandas, that has special index columns
row_headers: FieldTypes
field_types: FieldTypes
+ # Column types at each transform step (index 0 = original, index N = after N transforms)
+ column_types_per_step: list[FieldTypes]
python_code: Optional[str] = None
sql_code: Optional[str] = None
@@ -164,6 +166,9 @@ class dataframe(UIElement[dict[str, Any], DataFrameType]):
self._transform_container = TransformsContainer(nw_df, handler)
self._error: Optional[str] = None
self._last_transforms = Transformations([])
+ self._column_types_per_step: list[FieldTypes] = [
+ self._manager.get_field_types()
+ ]
self._page_size = page_size or 5 # Default to 5 rows (.head())
self._show_download = show_download
rows = self._manager.get_num_rows(force=False)
@@ -234,6 +239,7 @@ class dataframe(UIElement[dict[str, Any], DataFrameType]):
total_rows=response.total_rows,
row_headers=manager.get_row_headers(),
field_types=manager.get_field_types(),
+ column_types_per_step=self._column_types_per_step,
python_code=self._handler.as_python_code(
self._transform_container._snapshot_df,
self._dataframe_name,
@@ -277,7 +283,10 @@ class dataframe(UIElement[dict[str, Any], DataFrameType]):
try:
transformations = parse_raw(value, Transformations)
- result = self._transform_container.apply(transformations)
+ result, self._column_types_per_step = (
+ self._transform_container.apply(transformations)
+ )
+
self._error = None
self._last_transforms = transformations
return self._undo(result)
diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/apply.py b/marimo/_plugins/ui/_impl/dataframes/transforms/apply.py
index 1bb20a28e7..113a9ed823 100644
--- a/marimo/_plugins/ui/_impl/dataframes/transforms/apply.py
+++ b/marimo/_plugins/ui/_impl/dataframes/transforms/apply.py
@@ -13,6 +13,8 @@ from marimo._plugins.ui._impl.dataframes.transforms.types import (
TransformHandler,
TransformType,
)
+from marimo._plugins.ui._impl.tables.table_manager import FieldTypes
+from marimo._plugins.ui._impl.tables.utils import get_table_manager
from marimo._utils.assert_never import assert_never
from marimo._utils.narwhals_utils import can_narwhalify, make_lazy
@@ -114,29 +116,45 @@ class TransformsContainer:
self._snapshot_df = df
self._handler = handler
self._transforms: list[Transform] = []
+ self._field_types_cache: list[FieldTypes] = []
- def apply(self, transform: Transformations) -> nw.LazyFrame[IntoLazyFrame]:
+ def apply(
+ self, transform: Transformations
+ ) -> tuple[nw.LazyFrame[IntoLazyFrame], list[FieldTypes]]:
"""
Applies the given transformations to the dataframe.
+
+ Returns:
+ Tuple of (final_dataframe, field_types_per_step).
+ field_types_per_step[0] = original, field_types_per_step[N] = after N transforms.
"""
# If the new transformations are a superset of the existing ones,
# then we can just apply the new ones to the snapshot dataframe.
if self._is_superset(transform):
transforms_to_apply = self._get_next_transformations(transform)
- self._snapshot_df = _apply_transforms(
- self._snapshot_df, self._handler, transforms_to_apply
- )
- self._transforms = transform.transforms
- return self._snapshot_df
+ df = self._snapshot_df
+ field_types = list(self._field_types_cache)
- # If the new transformations are not a superset of the existing ones,
- # then we need to start from the original dataframe.
+ for t in transforms_to_apply.transforms:
+ df = _handle(df, self._handler, t)
+ field_types.append(get_table_manager(df).get_field_types())
+
+ self._snapshot_df = df
else:
- self._snapshot_df = _apply_transforms(
- self._original_df, self._handler, transform
- )
- self._transforms = transform.transforms
- return self._snapshot_df
+ field_types = [
+ get_table_manager(self._original_df).get_field_types()
+ ]
+ df = self._original_df
+
+ for t in transform.transforms:
+ df = _handle(df, self._handler, t)
+ field_types.append(get_table_manager(df).get_field_types())
+
+ self._snapshot_df = df
+
+ self._transforms = transform.transforms
+ self._field_types_cache = field_types
+ return df, field_types
def _is_superset(self, transforms: Transformations) -> bool:
"""
diff --git a/tests/_plugins/ui/_impl/dataframes/test_dataframe.py b/tests/_plugins/ui/_impl/dataframes/test_dataframe.py
index b115b675a9..8b6f7920bd 100644
--- a/tests/_plugins/ui/_impl/dataframes/test_dataframe.py
+++ b/tests/_plugins/ui/_impl/dataframes/test_dataframe.py
@@ -498,7 +498,10 @@ class TestDataframes:
aggregation_column_ids=["age"],
)
transformations = Transformations([transform])
- transformed_df = transform_container.apply(transformations)
+ transformed_df, field_types_at_steps = transform_container.apply(
+ transformations
+ )
+ assert len(field_types_at_steps) == 2 # original + 1 transform
# Verify the transformed DataFrame
df = transformed_df.collect().to_native()
@@ -585,7 +588,10 @@ class TestDataframes:
)
transformations = Transformations([transform_grp, transform_sort])
- transformed_df = transform_container.apply(transformations)
+ transformed_df, field_types_at_steps = transform_container.apply(
+ transformations
+ )
+ assert len(field_types_at_steps) == 3 # original + 2 transforms
# from Ibis to Polars
transformed_df = transformed_df.collect().to_polars()
diff --git a/tests/_plugins/ui/_impl/dataframes/test_handlers.py b/tests/_plugins/ui/_impl/dataframes/test_handlers.py
index 41f6a0ff51..32dabb4757 100644
--- a/tests/_plugins/ui/_impl/dataframes/test_handlers.py
+++ b/tests/_plugins/ui/_impl/dataframes/test_handlers.py
@@ -1222,7 +1222,15 @@ class TestTransformHandler:
)
# Apply the transformations
- result = container.apply(transformations)
+ result, field_types = container.apply(transformations)
+
+ # Verify field_types: original + 2 transforms = 3 entries
+ assert len(field_types) == 3
+ # All steps should have columns A and B
+ for ft in field_types:
+ col_names = [name for name, _ in ft]
+ assert "A" in col_names
+ assert "B" in col_names
# Get the transformed dataframe
# Check that the transformations were applied correctly
@@ -1242,9 +1250,12 @@ class TestTransformHandler:
assert container._get_next_transformations(
transformations
) == Transformations([filter_again_transform])
- result = container.apply(
+ result, field_types = container.apply(
transformations,
)
+ # Verify field_types: original + 3 transforms = 4 entries
+ assert len(field_types) == 4
+
# Check that the transformations were applied correctly
assert_frame_equal(undo(result), expected2)
@@ -1256,12 +1267,82 @@ class TestTransformHandler:
== transformations
)
# Reapply by removing the last transform
- result = container.apply(
+ result, field_types = container.apply(
transformations,
)
+ # Verify field_types: original + 2 transforms = 3 entries
+ assert len(field_types) == 3
+
# Check that the transformations were applied correctly
assert_frame_equal(undo(result), expected)
+ @staticmethod
+ @pytest.mark.parametrize(
+ "df",
+ create_test_dataframes({"A": [1, 2, 3], "B": ["x", "y", "z"]}),
+ )
+ def test_apply_field_types_with_rename(df: DataFrameType) -> None:
+ """Test that field types correctly reflect column renames."""
+ nw_df, _ = make_lazy(df)
+ container = TransformsContainer(nw_df, NarwhalsTransformHandler())
+
+ rename_transform = RenameColumnTransform(
+ type=TransformType.RENAME_COLUMN,
+ column_id="A",
+ new_column_id="C",
+ )
+ transformations = Transformations([rename_transform])
+ _, field_types = container.apply(transformations)
+
+ # Should have 2 entries: original and after rename
+ assert len(field_types) == 2
+
+ # Original should have A and B
+ original_cols = [name for name, _ in field_types[0]]
+ assert "A" in original_cols
+ assert "B" in original_cols
+ assert "C" not in original_cols
+
+ # After rename should have C and B (no A)
+ renamed_cols = [name for name, _ in field_types[1]]
+ assert "A" not in renamed_cols
+ assert "B" in renamed_cols
+ assert "C" in renamed_cols
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ "df",
+ create_test_dataframes(
+ {"group": ["a", "a", "b"], "value": [1, 2, 3]},
+ ),
+ )
+ def test_apply_field_types_with_groupby(df: DataFrameType) -> None:
+ """Test that field types correctly reflect group by aggregation."""
+ nw_df, _ = make_lazy(df)
+ container = TransformsContainer(nw_df, NarwhalsTransformHandler())
+
+ groupby_transform = GroupByTransform(
+ type=TransformType.GROUP_BY,
+ column_ids=["group"],
+ drop_na=False,
+ aggregation="sum",
+ aggregation_column_ids=[],
+ )
+ transformations = Transformations([groupby_transform])
+ _, field_types_at_steps = container.apply(transformations)
+
+ # Should have 2 entries: original and after group by
+ assert len(field_types_at_steps) == 2
+ assert field_types_at_steps == [
+ # Original should have group and value
+ [("group", ("string", "String")), ("value", ("integer", "Int64"))],
+ # After group by should have group and value_sum
+ [
+ ("group", ("string", "String")),
+ ("value_sum", ("integer", "Int64")),
+ ],
+ ]
+
@staticmethod
@pytest.mark.parametrize(
("df", "expected"),