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: convert time duration to numeric for column explorer (#6087)
Shahmir Varqha50fb6609a911
diff --git a/marimo/_data/preview_column.py b/marimo/_data/preview_column.pyindex cc4d9493fd..957e11c250 100644--- a/marimo/_data/preview_column.py+++ b/marimo/_data/preview_column.py@@ -264,11 +264,10 @@ def _get_altair_chart(# Filter the data to the column we wantcolumn_data = table.select_columns([column_name]).data+ column_data = _sanitize_data(column_data, column_name)if isinstance(column_data, nw.LazyFrame):column_data = column_data.collect()- column_data = _sanitize_dtypes(column_data, column_name)-error: Optional[str] = Nonemissing_packages: Optional[list[str]] = None@@ -330,25 +329,87 @@ def _get_chart_spec()-def _sanitize_dtypes(- column_data: nw.DataFrame[Any] | Any, column_name: str-) -> nw.DataFrame[Any] | Any:- """Sanitize dtypes for vegafusion"""+def _sanitize_data(+ column_data: nw.DataFrame[Any] | nw.LazyFrame[Any] | Any, column_name: str+) -> nw.DataFrame[Any] | nw.LazyFrame[Any] | Any:+ """+ Sanitize data for vegafusion.+ Vegafusion doesn't support all data types so we convert them to supported types.+ """try:- dtype = column_data.schema[column_name]+ frame = column_data.lazy()+ col = nw.col(column_name)+ dtype = column_data.collect_schema()[column_name]+if dtype == nw.Categorical or dtype == nw.Enum:- column_data = column_data.with_columns(- nw.col(column_name).cast(nw.String)- )+ column_data = frame.with_columns(col.cast(nw.String))# Int128 and UInt128 are not supported by datafusionelif dtype == nw.Int128:- column_data = column_data.with_columns(- nw.col(column_name).cast(nw.Int64)- )+ column_data = frame.with_columns(col.cast(nw.Int64))elif dtype == nw.UInt128:- column_data = column_data.with_columns(- nw.col(column_name).cast(nw.UInt64)- )+ column_data = frame.with_columns(col.cast(nw.UInt64))+ elif dtype == nw.Duration:+ # Convert Duration to numeric values for better charting support+ try:+ result = (+ frame.select(+ col.min().alias("min"), col.max().alias("max")+ )+ .collect()+ .rows(named=True)[0]+ )+ min_value = result["min"]+ max_value = result["max"]+ if min_value is not None and max_value is not None:+ diff = max_value - min_value+ total_seconds = diff.total_seconds()+ if total_seconds >= 604800:+ # Use weeks if range is at least a week+ column_data = column_data.with_columns(+ (col.dt.total_seconds() / 604800).alias(+ column_name+ )+ )+ elif total_seconds >= 86400:+ # Use days if range is at least a day+ column_data = column_data.with_columns(+ (col.dt.total_seconds() / 86400).alias(column_name)+ )+ elif total_seconds >= 3600:+ # Use hours if range is at least an hour+ column_data = column_data.with_columns(+ (col.dt.total_seconds() / 3600).alias(column_name)+ )+ elif total_seconds >= 60:+ # Use minutes if range is at least a minute+ column_data = column_data.with_columns(+ col.dt.total_minutes().alias(column_name)+ )+ elif total_seconds >= 1:+ # Use seconds if range is at least a second+ column_data = column_data.with_columns(+ col.dt.total_seconds().alias(column_name)+ )+ elif total_seconds >= 0.001:+ # Use milliseconds if range is at least a millisecond+ column_data = column_data.with_columns(+ col.dt.total_milliseconds().alias(column_name)+ )+ elif total_seconds >= 0.000001:+ # Use microseconds if range is at least a microsecond+ column_data = column_data.with_columns(+ col.dt.total_microseconds().alias(column_name)+ )+ elif total_seconds >= 0.000000001:+ # Use nanoseconds if range is at least a nanosecond+ column_data = column_data.with_columns(+ col.dt.total_nanoseconds().alias(column_name)+ )+ except Exception as e:+ LOGGER.warning("Failed to infer duration precision: %s", e)+ column_data = column_data.with_columns(+ col.dt.total_seconds().alias(column_name)+ )except Exception as e:LOGGER.warning(f"Failed to sanitize dtypes: {e}")return column_datadiff --git a/marimo/_server/ai/constants.py b/marimo/_server/ai/constants.pyindex ae4ac373ac..a976d44bd1 100644--- a/marimo/_server/ai/constants.py+++ b/marimo/_server/ai/constants.py@@ -1,2 +1,3 @@+# Copyright 2025 Marimo. All rights reserved.DEFAULT_MAX_TOKENS = 4096DEFAULT_MODEL = "openai/gpt-4o"diff --git a/marimo/_server/ai/ids.py b/marimo/_server/ai/ids.pyindex 8a365372c3..7ccc3fa416 100644--- a/marimo/_server/ai/ids.py+++ b/marimo/_server/ai/ids.py@@ -1,3 +1,4 @@+# Copyright 2025 Marimo. All rights reserved.from dataclasses import dataclassfrom typing import NewTypediff --git a/tests/_data/test_preview_column.py b/tests/_data/test_preview_column.pyindex ad386cb3ee..be888cc650 100644--- a/tests/_data/test_preview_column.py+++ b/tests/_data/test_preview_column.py@@ -1,13 +1,13 @@from __future__ import annotations-from datetime import datetime, time+from datetime import datetime, time, timedeltafrom typing import TYPE_CHECKINGfrom unittest.mock import patchimport pytestfrom marimo._data.preview_column import (- _sanitize_dtypes,+ _sanitize_data,get_column_preview_dataset,get_column_preview_for_dataframe,get_column_preview_for_duckdb,@@ -488,11 +488,11 @@ def test_sanitize_dtypes() -> None:nw_df = nw.from_native(df)# Sanitize the dtypes- result = _sanitize_dtypes(nw_df, "cat_col")- assert result.schema["cat_col"] == nw.String+ result = _sanitize_data(nw_df, "cat_col")+ assert result.collect_schema()["cat_col"] == nw.String- result = _sanitize_dtypes(nw_df, "int128_col")- assert result.schema["int128_col"] == nw.Int64+ result = _sanitize_data(nw_df, "int128_col")+ assert result.collect_schema()["int128_col"] == nw.Int64@pytest.mark.skipif(@@ -511,5 +511,41 @@ def test_sanitize_dtypes_enum() -> None:)nw_df = nw.from_native(df)- result = _sanitize_dtypes(nw_df, "enum_col")+ result = _sanitize_data(nw_df, "enum_col")assert result.schema["enum_col"] == nw.String+++ not DependencyManager.polars.has(), reason="polars not installed"+)+def test_preview_column_duration_dtype() -> None:+ import polars as pl++ # Test days conversion+ df = pl.DataFrame(+ {+ "duration_weeks": [timedelta(weeks=1), timedelta(weeks=2)],+ "duration_days": [timedelta(days=1), timedelta(days=2)],+ "duration_hours": [timedelta(hours=1), timedelta(hours=2)],+ "duration_minutes": [timedelta(minutes=1), timedelta(minutes=2)],+ "duration_seconds": [timedelta(seconds=1), timedelta(seconds=2)],+ "duration_milliseconds": [+ timedelta(milliseconds=1),+ timedelta(milliseconds=2),+ ],+ "duration_microseconds": [+ timedelta(microseconds=1),+ timedelta(microseconds=2),+ ],+ }+ )++ for column_name in df.columns:+ result = get_column_preview_dataset(+ table=get_table_manager(df),+ table_name="table",+ column_name=column_name,+ )+ assert result is not None+ assert result.chart_code is not None+ assert result.chart_spec is not None