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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: ensure datetime exports include millisecond precision in CSV output (#7244)
Myles Scolnicka492eaf6c50b
diff --git a/marimo/_plugins/ui/_impl/tables/pandas_table.py b/marimo/_plugins/ui/_impl/tables/pandas_table.pyindex 90f535f921..fe3c3b0491 100644--- a/marimo/_plugins/ui/_impl/tables/pandas_table.py+++ b/marimo/_plugins/ui/_impl/tables/pandas_table.py@@ -77,18 +77,9 @@ class PandasTableManagerFactory(TableManagerFactory):self, format_mapping: Optional[FormatMapping] = None) -> str:has_headers = len(self.get_row_headers()) > 0- # Pandas omits H:M:S for datetimes when H:M:S is identically- # 0; this doesn't play well with our frontend table component,- # so we use an explicit date format.return self.apply_formatting(format_mapping- )._original_data.to_csv(- # By adding %H:%M:%S and %z, we ensure that the- # datetime is displayed in the frontend with the- # correct timezone.- index=has_headers,- date_format="%Y-%m-%d %H:%M:%S%z",- )+ )._original_data.to_csv(index=has_headers)def to_json_str(self, format_mapping: Optional[FormatMapping] = Nonediff --git a/marimo/_smoke_tests/tables/milliseconds.py b/marimo/_smoke_tests/tables/milliseconds.pynew file mode 100644index 0000000000..a80e28ab34--- /dev/null+++ b/marimo/_smoke_tests/tables/milliseconds.py@@ -0,0 +1,79 @@+import marimo++__generated_with = "0.18.0"+app = marimo.App(width="medium")+++def _():+ import marimo as mo+ return (mo,)+++def _():+ import pandas as pd++ base = pd.Timestamp("2024-11-21T12:34:56")+ data = {+ "timestamp": [+ base,+ base + pd.Timedelta(milliseconds=0),+ base + pd.Timedelta(milliseconds=4),+ base + pd.Timedelta(milliseconds=9),+ base + pd.Timedelta(milliseconds=99),+ base + pd.Timedelta(milliseconds=999),+ ],+ }+ df = pd.DataFrame(data)+ pandas_dates_df = df+ pandas_dates_df+ return (pandas_dates_df,)+++def _(pandas_dates_df):+ import polars as pl++ pl.DataFrame(pandas_dates_df)+ return+++def _(mo):+ df_sql_dates = mo.sql(+ f"""+ SELECT raw,+ TRY_CAST(raw AS TIMESTAMP) AS parsed,+ raw LIKE '%.%' AS has_millis+ FROM (VALUES+ ('2023-01-01 12:00:00'),+ ('2023-01-01 12:00:00.123'),+ ('2023-06-30T23:59:59'),+ ('2023-06-30T23:59:59.999'),+ ('2022-12-31 00:00:00.000'),+ ('2022-12-31 00:00:00'),+ ('2023-05-01T08:30:00.456Z'),+ ('2023-05-01T08:30:00Z')+ ) AS t(raw)+ """+ )+ return++[email protected](hide_code=True)+def _(mo):+ mo.md(f"""+ Created a pandas DataFrame (pandas_dates_df) containing a variety of date strings — some with milliseconds and some without.+ Columns:+ - raw: original string+ - parsed: parsed timestamp (NaT if parsing failed)+ - has_millis: boolean indicating presence of fractional seconds++ Also created an equivalent SQL result (df_sql_dates) using an inline VALUES list and TRY_CAST to show parsed timestamps and a simple LIKE check to detect milliseconds.+ """)+ return+++if __name__ == "__main__":+ app.run()diff --git a/tests/_plugins/ui/_impl/tables/snapshots/pandas.csv b/tests/_plugins/ui/_impl/tables/snapshots/pandas.csvindex 1e712886c7..204cb48224 100644--- a/tests/_plugins/ui/_impl/tables/snapshots/pandas.csv+++ b/tests/_plugins/ui/_impl/tables/snapshots/pandas.csv@@ -1,4 +1,4 @@strings,bool,int,float,datetime,date,struct,list,nulls,category,set,imaginary,time,duration,mixed_list,bytes-a,True,1,1.0,2021-01-01 00:00:00,2021-01-01,"{'a': 1, 'b': 2}","[1, 2]",,cat,"{1, 2}",(1+2j),12:30:00,1 days,"[1, 'two']",b'\x00\x00\x00\x00\x01\xc0U\xe8\xb1n1\xc0T@D\xf1?Bc\x95\x83'-b,False,2,2.0,2021-01-02 00:00:00,2021-01-02,"{'a': 3, 'b': 4}","[3, 4]",data,dog,"{3, 4}",(3+4j),13:45:00,2 days,"[3.0, False]",b'world'-c,True,3,3.0,2021-01-03 00:00:00,2021-01-03,"{'a': 5, 'b': 6}","[5, 6]",,mouse,"{5, 6}",(5+6j),14:15:00,3 days,"[None, datetime.datetime(2021, 1, 1, 0, 0)]",b'bytes'+a,True,1,1.0,2021-01-01,2021-01-01,"{'a': 1, 'b': 2}","[1, 2]",,cat,"{1, 2}",(1+2j),12:30:00,1 days,"[1, 'two']",b'\x00\x00\x00\x00\x01\xc0U\xe8\xb1n1\xc0T@D\xf1?Bc\x95\x83'+b,False,2,2.0,2021-01-02,2021-01-02,"{'a': 3, 'b': 4}","[3, 4]",data,dog,"{3, 4}",(3+4j),13:45:00,2 days,"[3.0, False]",b'world'+c,True,3,3.0,2021-01-03,2021-01-03,"{'a': 5, 'b': 6}","[5, 6]",,mouse,"{5, 6}",(5+6j),14:15:00,3 days,"[None, datetime.datetime(2021, 1, 1, 0, 0)]",b'bytes'diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.pyindex 08865e1b02..6d3687e78b 100644--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py@@ -128,9 +128,7 @@ class TestPandasTableManager(unittest.TestCase):assert self.factory.package_name() == "pandas"def test_to_csv(self) -> None:- expected_csv = self.data.to_csv(- index=False, date_format="%Y-%m-%d %H:%M:%S%z"- ).encode("utf-8")+ expected_csv = self.data.to_csv(index=False).encode("utf-8")assert self.manager.to_csv() == expected_csvdef test_to_csv_datetime(self) -> None:@@ -141,7 +139,7 @@ class TestPandasTableManager(unittest.TestCase):}df = pd.DataFrame(data)manager = PandasTableManagerFactory.create()(df)- assert "2024-12-17 00:00:00" in manager.to_csv().decode("utf-8")+ assert "2024-12-17" in manager.to_csv().decode("utf-8")def test_to_csv_datetime_with_timezone(self) -> None:D = pd.to_datetime("2024-12-17", errors="coerce").tz_localize("UTC")@@ -151,7 +149,55 @@ class TestPandasTableManager(unittest.TestCase):}df = pd.DataFrame(data)manager = PandasTableManagerFactory.create()(df)- assert "2024-12-17 00:00:00+0000" in manager.to_csv().decode("utf-8")+ assert "2024-12-17 00:00:00+00:00" in manager.to_csv().decode("utf-8")++ # Different timezone+ D = pd.to_datetime("2024-12-17", errors="coerce").tz_localize(+ "America/New_York"+ )+ data = {+ "D timestamp": [D],+ }+ df = pd.DataFrame(data)+ manager = PandasTableManagerFactory.create()(df)+ assert "2024-12-17 00:00:00-05:00" in manager.to_csv().decode("utf-8")++ def test_to_csv_datetime_with_milliseconds(self) -> None:+ # Test that millisecond precision is preserved in CSV export+ base = pd.Timestamp("2024-11-21T12:34:56.123")+ data = {+ "event": ["start", "middle", "end"],+ "timestamp": [+ base,+ base + pd.Timedelta(milliseconds=4),+ base + pd.Timedelta(milliseconds=9),+ ],+ }+ df = pd.DataFrame(data)+ manager = PandasTableManagerFactory.create()(df)+ csv_output = manager.to_csv().decode("utf-8")++ # Verify millisecond precision is preserved+ assert "2024-11-21 12:34:56.123" in csv_output+ assert "2024-11-21 12:34:56.127" in csv_output+ assert "2024-11-21 12:34:56.132" in csv_output++ def test_to_csv_datetime_with_nanoseconds(self) -> None:+ base = pd.Timestamp("2024-11-21T12:34:56.123456789")+ data = {+ "event": ["start", "middle", "end"],+ "timestamp": [+ base,+ base + pd.Timedelta(nanoseconds=4),+ base + pd.Timedelta(nanoseconds=9),+ ],+ }+ df = pd.DataFrame(data)+ manager = PandasTableManagerFactory.create()(df)+ csv_output = manager.to_csv().decode("utf-8")+ assert "2024-11-21 12:34:56.123456789" in csv_output+ assert "2024-11-21 12:34:56.123456793" in csv_output+ assert "2024-11-21 12:34:56.123456798" in csv_outputdef test_to_csv_complex(self) -> None:complex_data = self.get_complex_data()