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: handle timezone in samples (#3584)
Myles Scolnick34f46bc37edc
diff --git a/marimo/_plugins/ui/_impl/tables/narwhals_table.py b/marimo/_plugins/ui/_impl/tables/narwhals_table.pyindex fefc258c23..0d3ebbd603 100644--- a/marimo/_plugins/ui/_impl/tables/narwhals_table.py+++ b/marimo/_plugins/ui/_impl/tables/narwhals_table.py@@ -286,7 +286,13 @@ class NarwhalsTableManager(return self.nw_schema.names()def get_unique_column_values(self, column: str) -> list[str | int | float]:- return self.data[column].unique().to_list()+ try:+ return self.data[column].unique().to_list()+ except BaseException:+ # Catch-all: some libraries like Polars have bugs and raise+ # BaseExceptions, which shouldn't crash the kernel+ # If an exception occurs, try converting to strings first+ return self.data[column].cast(nw.String).unique().to_list()def get_sample_values(self, column: str) -> list[str | int | float]:# Sample 3 values from the column@@ -305,10 +311,23 @@ class NarwhalsTableManager(return valuereturn str(value)- values = self.data[column].head(SAMPLE_SIZE).to_list()+ if self.data[column].dtype == nw.Datetime:+ # Drop timezone info for datetime columns+ # It's ok to drop timezone since these are just sample values+ # and not used for any calculations+ values = (+ self.data[column]+ .dt.replace_time_zone(None)+ .head(SAMPLE_SIZE)+ .to_list()+ )+ else:+ values = self.data[column].head(SAMPLE_SIZE).to_list()# Serialize values to primitivesreturn [to_primitive(v) for v in values]- except Exception:+ except BaseException:+ # Catch-all: some libraries like Polars have bugs and raise+ # BaseExceptions, which shouldn't crash the kernel# May be metadata-only framereturn []diff --git a/tests/_plugins/ui/_impl/tables/test_narwhals.py b/tests/_plugins/ui/_impl/tables/test_narwhals.pyindex c0ee676f49..7ba60ef229 100644--- a/tests/_plugins/ui/_impl/tables/test_narwhals.py+++ b/tests/_plugins/ui/_impl/tables/test_narwhals.py@@ -794,17 +794,102 @@ def test_sort_values_with_nulls(df: Any) -> None:@pytest.mark.parametrize("df",create_dataframes(- {"A": [1, 2, 3, 4], "B": ["a", "b", "c", "d"]},+ {+ "A": [1, 2, 3, 4], # Integer+ "B": ["a", "b", "c", "d"], # String+ "C": [+ datetime.datetime(2021, 1, 1, tzinfo=datetime.timezone.utc),+ datetime.datetime(2021, 1, 2, tzinfo=datetime.timezone.utc),+ datetime.datetime(2021, 1, 3, tzinfo=datetime.timezone.utc),+ datetime.datetime(2021, 1, 4, tzinfo=datetime.timezone.utc),+ ], # Datetime with timezone+ "D": [1.1, 2.2, 3.3, 4.4], # Float+ "E": [True, False, True, False], # Boolean+ "F": [None, "b", "c", None], # Mixed with nulls+ "G": [+ datetime.date(2021, 1, 1),+ datetime.date(2021, 1, 2),+ datetime.date(2021, 1, 3),+ datetime.date(2021, 1, 4),+ ], # Date+ "H": [10_000, -5_000, 0, 999_999], # Large integers+ "I": [+ float("inf"),+ float("-inf"),+ float("nan"),+ 1.0,+ ], # Special floats+ "J": ["", " ", "test", "\t\n"], # Whitespace strings+ "K": [b"bytes1", b"bytes2", b"bytes3", b"bytes4"], # Bytes+ },exclude=["ibis", "duckdb"],),)def test_get_sample_values(df: Any) -> None:manager = NarwhalsTableManager.from_dataframe(df)++ # Integersample_values = manager.get_sample_values("A")assert sample_values == [1, 2, 3]++ # Stringsample_values = manager.get_sample_values("B")assert sample_values == ["a", "b", "c"]+ # Datetime with timezone+ sample_values = manager.get_sample_values("C")+ assert (+ sample_values+ == [+ "2021-01-01 00:00:00",+ "2021-01-02 00:00:00",+ "2021-01-03 00:00:00",+ ]+ # Polars on windows is missing timezone info+ or sample_values == []+ )++ # Float+ sample_values = manager.get_sample_values("D")+ assert sample_values == [1.1, 2.2, 3.3]++ # Boolean+ sample_values = manager.get_sample_values("E")+ assert sample_values == [True, False, True]++ # Mixed with nulls+ sample_values = manager.get_sample_values("F")+ assert sample_values == ["None", "b", "c"]++ # Date+ sample_values = manager.get_sample_values("G")+ # Polars on windows is missing timezone info+ assert sample_values == [+ "2021-01-01",+ "2021-01-02",+ "2021-01-03",+ ] or sample_values == [+ "2021-01-01 00:00:00",+ "2021-01-02 00:00:00",+ "2021-01-03 00:00:00",+ ]++ # Large integers+ sample_values = manager.get_sample_values("H")+ assert sample_values == [10_000, -5_000, 0]++ # Special floats+ sample_values = manager.get_sample_values("I")+ assert len(sample_values) == 3++ # Whitespace strings+ sample_values = manager.get_sample_values("J")+ assert sample_values == ["", " ", "test"]++ # Bytes+ sample_values = manager.get_sample_values("K")+ assert sample_values == ["b'bytes1'", "b'bytes2'", "b'bytes3'"]+@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")@pytest.mark.parametrize(