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.

fix: support more pandas dtypes in dataframe viewer (#5660)

Myles Scolnick76aadfbbd1c5
diff --git a/frontend/src/plugins/impl/data-frames/types.ts b/frontend/src/plugins/impl/data-frames/types.ts
index 3add62304f..f365f37a9d 100644
--- a/frontend/src/plugins/impl/data-frames/types.ts
+++ b/frontend/src/plugins/impl/data-frames/types.ts
@@ -44,9 +44,11 @@ export const NUMPY_DTYPES = [
"timedelta64",
] as const;
-export function numpyTypeToDataType(
- nptype: (typeof NUMPY_DTYPES)[number],
-): DataType {
+type NumpyType = (typeof NUMPY_DTYPES)[number];
+
+export function numpyTypeToDataType(nptype: NumpyType): DataType {
+ nptype = nptype.toLowerCase() as NumpyType;
+
switch (nptype) {
case "int8":
case "int16":
diff --git a/marimo/_plugins/ui/_impl/tables/pandas_table.py b/marimo/_plugins/ui/_impl/tables/pandas_table.py
index 4b578c07c7..2fbd59a23b 100644
--- a/marimo/_plugins/ui/_impl/tables/pandas_table.py
+++ b/marimo/_plugins/ui/_impl/tables/pandas_table.py
@@ -244,27 +244,33 @@ class PandasTableManagerFactory(TableManagerFactory):
else:
dtype = str(dtype)
- if dtype.startswith("interval"):
+ lower_dtype = dtype.lower()
+
+ if lower_dtype.startswith("interval"):
return ("string", dtype)
- if dtype.startswith("int") or dtype.startswith("uint"):
+ if lower_dtype.startswith("int") or lower_dtype.startswith(
+ "uint"
+ ):
return ("integer", dtype)
- if dtype.startswith("float"):
+ if lower_dtype.startswith("float"):
return ("number", dtype)
- if dtype == "object":
+ if lower_dtype == "object":
return ("string", dtype)
- if dtype == "bool":
+ if lower_dtype == "bool":
return ("boolean", dtype)
- if dtype.startswith("datetime"):
+ if lower_dtype.startswith("datetime"):
return ("datetime", dtype)
- if dtype == "date":
+ if lower_dtype == "date":
return ("date", dtype)
- if dtype == "time":
+ if lower_dtype == "time":
return ("time", dtype)
- if dtype == "timedelta64[ns]":
+ if lower_dtype == "timedelta64[ns]":
+ return ("string", dtype)
+ if lower_dtype == "category":
return ("string", dtype)
- if dtype == "category":
+ if lower_dtype == "string":
return ("string", dtype)
- if dtype.startswith("complex"):
+ if lower_dtype.startswith("complex"):
return ("unknown", dtype)
return ("unknown", dtype)
diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.py
index 7bb9e0aa69..7c2b28a135 100644
--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py
+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py
@@ -461,6 +461,25 @@ class TestPandasTableManager(unittest.TestCase):
== expected_field_types
)
+ def test_get_field_types_nullables(self) -> None:
+ data = pd.DataFrame(
+ {
+ "A": [1.0, 2.0, 3.0],
+ "B": ["a", "b", "c"],
+ }
+ )
+ float64_cols = data.select_dtypes(include="float64").columns
+ data[float64_cols] = data[float64_cols].astype("Float64")
+ object_cols = data.select_dtypes(include=["object"]).columns
+ data[object_cols] = data[object_cols].astype("string")
+
+ manager = self.factory.create()(data)
+ field_types = manager.get_field_types()
+ assert field_types == [
+ ("A", ("number", "Float64")),
+ ("B", ("string", "string")),
+ ]
+
@pytest.mark.xfail(
reason="Narwhals (wrapped pandas) doesn't support duplicate columns",
)