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: datetime formatting for Pandas tables (#3383)
Akshay Agrawal0c7b786669e7
diff --git a/marimo/_plugins/ui/_impl/tables/pandas_table.py b/marimo/_plugins/ui/_impl/tables/pandas_table.pyindex 239f37b0fb..0be77bdb62 100644--- a/marimo/_plugins/ui/_impl/tables/pandas_table.py+++ b/marimo/_plugins/ui/_impl/tables/pandas_table.py@@ -45,9 +45,14 @@ class PandasTableManagerFactory(TableManagerFactory):self, format_mapping: Optional[FormatMapping] = None) -> bytes: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(index=has_headers)+ ._original_data.to_csv(+ index=has_headers, date_format="%Y-%m-%d %H:%M:%S"+ ).encode("utf-8"))diff --git a/tests/_plugins/ui/_impl/charts/test_altair_transformers.py b/tests/_plugins/ui/_impl/charts/test_altair_transformers.pyindex e7bf554d9b..a48f2d0dc4 100644--- a/tests/_plugins/ui/_impl/charts/test_altair_transformers.py+++ b/tests/_plugins/ui/_impl/charts/test_altair_transformers.py@@ -201,9 +201,9 @@ def test_data_to_json_string_with_special_characters("float": [1.1, 2.2, 3.3],"bool": [True, False, True],"datetime": [- datetime.datetime(2023, 1, 1),- datetime.datetime(2023, 1, 2),- datetime.datetime(2023, 1, 3),+ datetime.datetime(2023, 1, 1, 1),+ datetime.datetime(2023, 1, 2, 1),+ datetime.datetime(2023, 1, 3, 1),],"category": ["a", "b", "c"],},@@ -213,6 +213,8 @@ def test_data_to_json_string_with_special_characters(def test_data_to_csv_string_with_different_dtypes(df: IntoDataFrame):result = _data_to_csv_string(df)assert isinstance(result, str)+ # NB: These can differ based on datetime canonicalization --- our table+ # manager always includes H:M:S:.f for pandas, but narwhals doesn't.assert result == dataframe_to_csv(df)diff --git a/tests/_plugins/ui/_impl/tables/snapshots/ibis.csv b/tests/_plugins/ui/_impl/tables/snapshots/ibis.csvindex 878da1dfaf..b09ce34dbc 100644--- a/tests/_plugins/ui/_impl/tables/snapshots/ibis.csv+++ b/tests/_plugins/ui/_impl/tables/snapshots/ibis.csv@@ -1,4 +1,4 @@strings,bool,int,float,date,datetime,time,nulls-a,True,1,1.0,2021-01-01,2021-01-01,01:02:03,-b,False,2,2.0,2021-01-02,2021-01-02,04:05:06,data-c,True,3,3.0,2021-01-03,2021-01-03,07:08:09,+a,True,1,1.0,2021-01-01,2021-01-01 00:00:00,01:02:03,+b,False,2,2.0,2021-01-02,2021-01-02 00:00:00,04:05:06,data+c,True,3,3.0,2021-01-03,2021-01-03 00:00:00,07:08:09,diff --git a/tests/_plugins/ui/_impl/tables/snapshots/pandas.csv b/tests/_plugins/ui/_impl/tables/snapshots/pandas.csvindex b671566766..55657ef34e 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-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,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]"-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)]"+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,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]"+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)]"diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.pyindex 431cd88936..e2ec05d91a 100644--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py@@ -120,9 +120,21 @@ class TestPandasTableManager(unittest.TestCase):assert self.factory.package_name() == "pandas"def test_to_csv(self) -> None:- expected_csv = self.data.to_csv(index=False).encode("utf-8")+ expected_csv = self.data.to_csv(+ index=False, date_format="%Y-%m-%d %H:%M:%S"+ ).encode("utf-8")assert self.manager.to_csv() == expected_csv+ def test_to_csv_datetime(self) -> None:+ D = pd.to_datetime("2024-12-17", errors="coerce")++ data = {+ "D timestamp": [D],+ }+ df = pd.DataFrame(data)+ manager = PandasTableManagerFactory.create()(df)+ assert "2024-12-17 00:00:00" in manager.to_csv().decode("utf-8")+def test_to_csv_complex(self) -> None:complex_data = self.get_complex_data()data = complex_data.to_csv()