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: altair timezone rendering (#3567)
Myles Scolnick854e6416c8c0
diff --git a/marimo/_plugins/ui/_impl/tables/pandas_table.py b/marimo/_plugins/ui/_impl/tables/pandas_table.pyindex 0be77bdb62..1e18158ba6 100644--- a/marimo/_plugins/ui/_impl/tables/pandas_table.py+++ b/marimo/_plugins/ui/_impl/tables/pandas_table.py@@ -51,7 +51,11 @@ class PandasTableManagerFactory(TableManagerFactory):return (self.apply_formatting(format_mapping)._original_data.to_csv(- index=has_headers, date_format="%Y-%m-%d %H:%M:%S"+ # 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",).encode("utf-8"))diff --git a/marimo/_smoke_tests/issues/2817_timezones.py b/marimo/_smoke_tests/issues/2871_timezones.pysimilarity index 80%rename from marimo/_smoke_tests/issues/2817_timezones.pyrename to marimo/_smoke_tests/issues/2871_timezones.pyindex 140a6a263b..bb884b92fd 100644--- a/marimo/_smoke_tests/issues/2817_timezones.py+++ b/marimo/_smoke_tests/issues/2871_timezones.py@@ -1,28 +1,30 @@import marimo-__generated_with = "0.9.19"+__generated_with = "0.10.16"app = marimo.App(width="medium")@app.cell-def __():+def _():import polars as plfrom datetime import datetime, dated = datetime(2010, 10, 7, 13, 15)+ # It should appear as 2010-10-07T13:15:00.000pl.DataFrame({"timestamp": [d], "date": [d.date()]})return d, date, datetime, pl@app.cell-def __(d):+def _(d):+ # It should appear as 2010-10-07T13:15:00.000[d, d.date()]return@app.cell-def __(date, pl):+def _(date, pl):import altair as altdata = pl.DataFrame(diff --git a/marimo/_smoke_tests/issues/3564_altair_timezone.py b/marimo/_smoke_tests/issues/3564_altair_timezone.pynew file mode 100644index 0000000000..26f1dc9ebe--- /dev/null+++ b/marimo/_smoke_tests/issues/3564_altair_timezone.py@@ -0,0 +1,80 @@+import marimo++__generated_with = "0.10.16"+app = marimo.App(width="medium")++[email protected](hide_code=True)+def _():+ import altair as alt+ import marimo as mo+ import pandas as pd+ import polars as pl+ from datetime import datetime, timedelta, timezone+ return alt, datetime, mo, pd, pl, timedelta, timezone++[email protected](hide_code=True)+def _(mo, pd, pl):+ df_lib_select = mo.ui.dropdown(+ {+ "pandas": pd,+ "polars": pl,+ },+ label="Dataframe library",+ value="pandas",+ )+ df_lib_select+ return (df_lib_select,)++[email protected](hide_code=True)+def _(datetime, timedelta, timezone):+ # just creates 10 data points every hour, starting from 2024-08-30 00:00:00.000Z+ to_display = [+ {+ "datetime": datetime(+ year=2024, month=8, day=30, hour=0, tzinfo=timezone.utc+ )+ + timedelta(hours=i),+ "count": i + 1,+ }+ for i in range(10)+ ]+ return (to_display,)+++def _(alt, df_lib_select, mo, timedelta, to_display):+ df_lib = df_lib_select.value++ df_buckets = df_lib.DataFrame(to_display)++ bars = (+ alt.Chart(df_buckets)+ .mark_bar(size=30)+ .encode(+ x=alt.X(+ "datetime:T",+ scale=alt.Scale(+ # it just helps to see the difference+ domain=[+ to_display[0]["datetime"] - timedelta(hours=3),+ to_display[-1]["datetime"] + timedelta(hours=1),+ ],+ type="utc",+ ),+ title="bucket",+ ),+ y="count:Q",+ )+ .properties(width=800, height=200)+ )+ chart = mo.ui.altair_chart(bars)++ mo.vstack([bars, chart])+ return bars, chart, df_buckets, df_lib+++if __name__ == "__main__":+ app.run()diff --git a/marimo/_smoke_tests/timezones.py b/marimo/_smoke_tests/timezones.pyindex 305a8d01b0..ea52070ea3 100644--- a/marimo/_smoke_tests/timezones.py+++ b/marimo/_smoke_tests/timezones.py@@ -1,53 +1,96 @@import marimo-__generated_with = "0.9.14"+__generated_with = "0.10.16"app = marimo.App(width="medium")@app.cell-def __():- import polars as pl+def _():+ import polars as pl_lib+ import pandas as pd_libfrom datetime import datetime, date, time+ import sys++ sys.tracebacklimit = 1+ return date, datetime, pd_lib, pl_lib, sys, time++[email protected](hide_code=True)+def _(mo, pd_lib, pl_lib):+ df_library = mo.ui.dropdown(+ {+ "Polars": pl_lib,+ "Pandas": pd_lib,+ },+ label="Dataframe library",+ value="Polars",+ )+ df_library+ return (df_library,)+++def _(df_library):+ pl = df_library.value+ return (pl,)++[email protected](hide_code=True)+def _():import marimo as mo++ def print_df(df):+ return mo.hstack([df, mo.plain(df)])+ return mo, print_df+++def _(datetime, pl, print_df):df = pl.DataFrame({"price": [40, 50],"datetime": [datetime(2020, 1, 1), datetime(2020, 1, 2)],})- df- return date, datetime, df, mo, pl, time++ print_df(df)+ return (df,)@app.cell-def __(df, pl):- df.with_columns(pl.col("datetime").dt.replace_time_zone("Asia/Kathmandu"))- return+def _(df, print_df):+ import narwhals as nw++ _df = (+ nw.from_native(df)+ .with_columns(nw.col("datetime").dt.replace_time_zone("Asia/Kathmandu"))+ .to_native()+ )++ print_df(_df)+ return (nw,)@app.cell-def __(date, pl):- df2 = pl.DataFrame(- {- "price": [40, 50],- "date": [date(2020, 1, 1), date(2020, 1, 2)],- }+def _(date, pl, print_df):+ _df = pl.DataFrame(+ {"price": [40, 50], "date": [date(2020, 1, 1), date(2020, 1, 2)]})- df2- return (df2,)+ print_df(_df)+ return@app.cell-def __(pl, time):- df3 = pl.DataFrame(+def _(pl, print_df, time):+ _df = pl.DataFrame({"price": [40, 50],"time": [time(1, 30), time(2, 30)],})- df3- return (df3,)+ print_df(_df)+ returnif __name__ == "__main__":diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.pyindex e2ec05d91a..a2723e8c02 100644--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py@@ -121,7 +121,7 @@ class TestPandasTableManager(unittest.TestCase):def test_to_csv(self) -> None:expected_csv = self.data.to_csv(- index=False, date_format="%Y-%m-%d %H:%M:%S"+ index=False, date_format="%Y-%m-%d %H:%M:%S%z").encode("utf-8")assert self.manager.to_csv() == expected_csv@@ -135,6 +135,16 @@ class TestPandasTableManager(unittest.TestCase):manager = PandasTableManagerFactory.create()(df)assert "2024-12-17 00:00:00" 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")++ data = {+ "D timestamp": [D],+ }+ df = pd.DataFrame(data)+ manager = PandasTableManagerFactory.create()(df)+ assert "2024-12-17 00:00:00+0000" 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()