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: handle empty or null dates in table (#2814)

Myles Scolnick22d8f1831daf
diff --git a/frontend/src/plugins/impl/vega/__tests__/vega.test.ts b/frontend/src/plugins/impl/vega/__tests__/vega.test.ts
index c716f131c8..bdd0487c86 100644
--- a/frontend/src/plugins/impl/vega/__tests__/vega.test.ts
+++ b/frontend/src/plugins/impl/vega/__tests__/vega.test.ts
@@ -353,6 +353,48 @@ Bob.Jones,25
]
`);
});
+
+ it("should handle null values", async () => {
+ const csvData = "name,age\nAlice,30\nBob,null";
+ vi.spyOn(vegaLoader, "load").mockReturnValue(Promise.resolve(csvData));
+ const data = await vegaLoadData(csvData, {
+ type: "csv",
+ parse: { age: "number", name: "string" },
+ });
+ expect(data).toMatchInlineSnapshot(`
+ [
+ {
+ "age": 30,
+ "name": "Alice",
+ },
+ {
+ "age": NaN,
+ "name": "Bob",
+ },
+ ]
+ `);
+ });
+
+ it("should handle empty dates", async () => {
+ const csvData = "name,created_at\nAlice,2024-01-01T00:00:00Z\nBob,";
+ vi.spyOn(vegaLoader, "load").mockReturnValue(Promise.resolve(csvData));
+ const data = await vegaLoadData(csvData, {
+ type: "csv",
+ parse: { created_at: "date", name: "string" },
+ });
+ expect(data).toMatchInlineSnapshot(`
+ [
+ {
+ "created_at": "2024-01-01T00:00:00.000Z",
+ "name": "Alice",
+ },
+ {
+ "created_at": "",
+ "name": "Bob",
+ },
+ ]
+ `);
+ });
});
describe("uniquifyColumnNames", () => {
diff --git a/frontend/src/plugins/impl/vega/loader.ts b/frontend/src/plugins/impl/vega/loader.ts
index 2be5ad4c62..afa438b8ad 100644
--- a/frontend/src/plugins/impl/vega/loader.ts
+++ b/frontend/src/plugins/impl/vega/loader.ts
@@ -3,6 +3,7 @@ import type { DataFormat } from "./types";
import { isNumber } from "lodash-es";
import { typeParsers, createLoader, read, type DataType } from "./vega-loader";
import { Objects } from "@/utils/objects";
+import { Logger } from "@/utils/Logger";
type Unsubscribe = () => void;
type Middleware = () => Unsubscribe;
@@ -62,7 +63,20 @@ const BIG_INT_MIDDLEWARE: Middleware = () => {
};
const DATE_MIDDLEWARE: Middleware = () => {
- typeParsers.date = (value: string) => new Date(value).toISOString();
+ typeParsers.date = (value: string) => {
+ if (value === "") {
+ return "";
+ }
+ if (value == null) {
+ return null;
+ }
+ try {
+ return new Date(value).toISOString();
+ } catch {
+ Logger.warn(`Failed to parse date: ${value}`);
+ return value;
+ }
+ };
return () => {
typeParsers.date = previousDateParser;
};
diff --git a/marimo/_smoke_tests/_polars/polars_date_column.py b/marimo/_smoke_tests/_polars/polars_date_column.py
new file mode 100644
index 0000000000..261ef61149
--- /dev/null
+++ b/marimo/_smoke_tests/_polars/polars_date_column.py
@@ -0,0 +1,26 @@
+import marimo
+
+__generated_with = "0.9.15"
+app = marimo.App(width="medium")
+
+
+def __():
+ import polars as pl
+ from datetime import date
+
+ df = (
+ pl.date_range(date(2001, 1, 1), date(2001, 1, 3), eager=True)
+ .alias("date")
+ .to_frame()
+ )
+ df.with_columns(
+ pl.col("date").dt.timestamp().alias("timestamp_us"),
+ pl.col("date").dt.timestamp("ms").alias("timestamp_ms"),
+ pl.lit(None).cast(pl.Datetime).alias("test"),
+ )
+ return date, df, pl
+
+
+if __name__ == "__main__":
+ app.run()