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.

fixes pandas dates including time information (#7197)

Shahmir Varqha288276f7c946
diff --git a/marimo/_plugins/ui/_impl/tables/pandas_table.py b/marimo/_plugins/ui/_impl/tables/pandas_table.py
index 001ca57487..90f535f921 100644
--- a/marimo/_plugins/ui/_impl/tables/pandas_table.py
+++ b/marimo/_plugins/ui/_impl/tables/pandas_table.py
@@ -114,6 +114,12 @@ class PandasTableManagerFactory(TableManagerFactory):
) or is_timedelta64_ns_dtype(dtype):
result[col] = result[col].apply(str)
if is_object_dtype(dtype):
+ # Check if column contains date objects (not datetime), and convert them to string
+ # Typically, this will change to YYYY-MM-DD format
+ inferred_dtype = self._infer_dtype(col)
+ if inferred_dtype == "date":
+ result[col] = result[col].apply(str)
+
result[col] = result[col].apply(
self._sanitize_table_value
)
diff --git a/marimo/_smoke_tests/pandas/dates.py b/marimo/_smoke_tests/pandas/dates.py
new file mode 100644
index 0000000000..eda73ff1dc
--- /dev/null
+++ b/marimo/_smoke_tests/pandas/dates.py
@@ -0,0 +1,103 @@
+import marimo
+
+__generated_with = "0.17.6"
+app = marimo.App(width="medium")
+
+
+def _():
+ import marimo as mo
+ import pandas as pd
+ from datetime import datetime
+ return datetime, mo, pd
+
+
+def _(mo, pd):
+ date_df = pd.DataFrame(
+ {
+ "dates": ["10/6/2025 14:17", "10/6/2025 14:18"],
+ "date_series": pd.Series(pd.date_range("2000", freq="D", periods=2)),
+ }
+ )
+ date_df["date_only"] = pd.to_datetime(date_df.dates).dt.date
+ mo.vstack([mo.plain(date_df), date_df])
+ return
+
+
+def _(datetime, mo, pd):
+ time = pd.DataFrame(
+ {
+ "s": [pd.to_datetime(1490195805, unit="s")],
+ "ns": [pd.to_datetime(1490195805433502912, unit="ns")],
+ "times": [
+ pd.to_datetime(
+ [1, 2, 3], unit="D", origin=pd.Timestamp("1960-01-01")
+ )
+ ],
+ "delta": [pd.to_timedelta(["1 days 06:05:01.00003", "15.5us", "nan"])],
+ "format": [
+ pd.to_datetime(
+ "2018-10-26 12:00:00.0000000011", format="%Y-%m-%d %H:%M:%S.%f"
+ )
+ ],
+ "not_a_time": [
+ pd.to_datetime("13000101", format="%Y%m%d", errors="coerce")
+ ],
+ "timezone": [
+ pd.to_datetime(
+ ["2018-10-26 12:00 -0500", "2018-10-26 13:00 -0500"]
+ )
+ ],
+ "mixed_timezone": [
+ pd.to_datetime(
+ ["2020-01-01 01:00:00-01:00", datetime(2020, 1, 1, 3, 0)]
+ )
+ ],
+ "with_utc_true": [
+ pd.to_datetime(
+ ["2018-10-26 12:00", datetime(2020, 1, 1, 18)], utc=True
+ )
+ ],
+ }
+ ).transpose()
+ mo.vstack([mo.plain(time), time])
+ return
+
+
+def _(mo, pd):
+ date_ranges = pd.DataFrame(
+ {
+ "date_range": [
+ pd.date_range(
+ start=pd.to_datetime("1/1/2018").tz_localize("Europe/Berlin"),
+ end=pd.to_datetime("1/08/2018").tz_localize("Europe/Berlin"),
+ )
+ ],
+ "bdate_range": [pd.bdate_range(start="1/1/2018", end="1/08/2018")],
+ "period_range": [
+ pd.period_range(
+ start=pd.Period("2017Q1", freq="Q"),
+ end=pd.Period("2017Q2", freq="Q"),
+ freq="M",
+ )
+ ],
+ "timedelta_range": [
+ pd.timedelta_range("1 Day", periods=3, freq="100000D", unit="s")
+ ],
+ "interval_range": [
+ pd.interval_range(
+ start=pd.Timestamp("2017-01-01"),
+ end=pd.Timestamp("2017-01-04"),
+ )
+ ],
+ }
+ ).transpose()
+ mo.vstack([mo.plain(date_ranges), date_ranges])
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_plugins/ui/_impl/tables/snapshots/pandas.json b/tests/_plugins/ui/_impl/tables/snapshots/pandas.json
index 17778bc8b3..5a14590311 100644
--- a/tests/_plugins/ui/_impl/tables/snapshots/pandas.json
+++ b/tests/_plugins/ui/_impl/tables/snapshots/pandas.json
@@ -3,9 +3,9 @@
"strings": "a",
"bool": true,
"int": 1,
- "float": 1,
+ "float": 1.0,
"datetime": "2021-01-01T00:00:00.000",
- "date": "2021-01-01T00:00:00.000",
+ "date": "2021-01-01",
"struct": {
"a": 1,
"b": 2
@@ -33,9 +33,9 @@
"strings": "b",
"bool": false,
"int": 2,
- "float": 2,
+ "float": 2.0,
"datetime": "2021-01-02T00:00:00.000",
- "date": "2021-01-02T00:00:00.000",
+ "date": "2021-01-02",
"struct": {
"a": 3,
"b": 4
@@ -54,7 +54,7 @@
"time": "13:45:00",
"duration": "2 days 00:00:00",
"mixed_list": [
- 3,
+ 3.0,
false
],
"bytes": "b'world'"
@@ -63,9 +63,9 @@
"strings": "c",
"bool": true,
"int": 3,
- "float": 3,
+ "float": 3.0,
"datetime": "2021-01-03T00:00:00.000",
- "date": "2021-01-03T00:00:00.000",
+ "date": "2021-01-03",
"struct": {
"a": 5,
"b": 6
diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.py
index 1d48d56314..08865e1b02 100644
--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py
+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py
@@ -1309,6 +1309,39 @@ class TestPandasTableManager(unittest.TestCase):
"Original DataFrame columns should still be Timestamps"
)
+ def test_to_json_date_objects_from_dt_date(self) -> None:
+ import pandas as pd
+
+ date_df = pd.DataFrame(
+ {"dates": ["10/6/2025 14:17", "10/7/2025 14:18"]}
+ )
+ date_df["date_only"] = pd.to_datetime(date_df.dates).dt.date
+
+ manager = self.factory.create()(date_df)
+ json_str = manager.to_json_str()
+ json_data = json.loads(json_str)
+
+ assert json_data[0]["date_only"] == "2025-10-06"
+ assert json_data[1]["date_only"] == "2025-10-07"
+
+ @pytest.mark.xfail(reason="date_series is recognized as datetime object")
+ def test_to_json_date_series_objects(self) -> None:
+ import pandas as pd
+
+ date_df = pd.DataFrame(
+ {
+ "date_series": pd.Series(
+ pd.date_range("2000", freq="D", periods=2)
+ ),
+ }
+ )
+ manager = self.factory.create()(date_df)
+ json_str = manager.to_json_str()
+ json_data = json.loads(json_str)
+
+ assert json_data[0]["date_series"] == "2000-01-01"
+ assert json_data[1]["date_series"] == "2000-01-02"
+
def test_handle_integer_column_names_no_mutation(self) -> None:
import pandas as pd