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: the groupby code generated for pandas (#1687)

Myles Scolnick93a8f1a330f8
diff --git a/frontend/src/plugins/impl/data-frames/python/__tests__/python-print.test.ts b/frontend/src/plugins/impl/data-frames/python/__tests__/python-print.test.ts
index d220cd7672..2dc41d545a 100644
--- a/frontend/src/plugins/impl/data-frames/python/__tests__/python-print.test.ts
+++ b/frontend/src/plugins/impl/data-frames/python/__tests__/python-print.test.ts
@@ -126,6 +126,28 @@ describe("pythonPrint", () => {
expect(result2).toMatchInlineSnapshot(
`"df.groupby(["my_column", "my_column2", 3]).sum()"`,
);
+
+ const transform3: TransformType = {
+ type: "group_by",
+ column_ids: ["my_column"] as ColumnId[],
+ aggregation: "mean",
+ drop_na: false,
+ };
+ const result3 = pythonPrint("df", transform3);
+ expect(result3).toMatchInlineSnapshot(
+ `"df.groupby(["my_column"]).mean(numeric_only=True)"`,
+ );
+
+ const transform4: TransformType = {
+ type: "group_by",
+ column_ids: ["my_column"] as ColumnId[],
+ aggregation: "median",
+ drop_na: false,
+ };
+ const result4 = pythonPrint("df", transform4);
+ expect(result4).toMatchInlineSnapshot(
+ `"df.groupby(["my_column"]).median(numeric_only=True)"`,
+ );
});
});
diff --git a/frontend/src/plugins/impl/data-frames/python/python-print.ts b/frontend/src/plugins/impl/data-frames/python/python-print.ts
index 2f47396fe7..9e081b257a 100644
--- a/frontend/src/plugins/impl/data-frames/python/python-print.ts
+++ b/frontend/src/plugins/impl/data-frames/python/python-print.ts
@@ -78,7 +78,13 @@ export function pythonPrint(dfName: string, transform: TransformType): string {
listOfStrings(column_ids),
drop_na ? "dropna=True" : "",
);
- return `${dfName}.groupby(${args}).${aggregation}()`;
+ let aggArgs = "";
+ if (aggregation === "mean") {
+ aggArgs = "numeric_only=True";
+ } else if (aggregation === "median") {
+ aggArgs = "numeric_only=True";
+ }
+ return `${dfName}.groupby(${args}).${aggregation}(${aggArgs})`;
}
case "select_columns": {
const { column_ids } = transform;