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
improvement: warn on integer columns names (#3005)
Myles Scolnick816983a44e16
diff --git a/marimo/_plugins/ui/_impl/dataframes/dataframe.py b/marimo/_plugins/ui/_impl/dataframes/dataframe.pyindex 93653634e2..e9c4b8d1f3 100644--- a/marimo/_plugins/ui/_impl/dataframes/dataframe.py+++ b/marimo/_plugins/ui/_impl/dataframes/dataframe.py@@ -36,6 +36,7 @@ from marimo._plugins.ui._impl.tables.table_manager import (from marimo._plugins.ui._impl.tables.utils import (get_table_manager,)+from marimo._plugins.validators import validate_no_integer_columnsfrom marimo._runtime.functions import EmptyArgs, Functionfrom marimo._utils.memoize import memoize_last_valuefrom marimo._utils.parse_dataclass import parse_raw@@ -110,6 +111,7 @@ class dataframe(UIElement[Dict[str, Any], DataFrameType]):page_size: Optional[int] = 5,limit: Optional[int] = None,) -> None:+ validate_no_integer_columns(df)# This will raise an error if the dataframe type is not supported.handler = get_handler_for_dataframe(df)diff --git a/marimo/_plugins/ui/_impl/table.py b/marimo/_plugins/ui/_impl/table.pyindex dd3f26032e..9f5a17200b 100644--- a/marimo/_plugins/ui/_impl/table.py+++ b/marimo/_plugins/ui/_impl/table.py@@ -38,6 +38,7 @@ from marimo._plugins.ui._impl.tables.table_manager import ()from marimo._plugins.ui._impl.tables.utils import get_table_managerfrom marimo._plugins.ui._impl.utils.dataframe import ListOrTuple, TableData+from marimo._plugins.validators import validate_no_integer_columnsfrom marimo._runtime.functions import EmptyArgs, Functionfrom marimo._utils.narwhals_utils import unwrap_narwhals_dataframe@@ -254,6 +255,8 @@ class table(UIElement[List[str], Union[List[JSONType], IntoDataFrame]]):_internal_summary_row_limit: Optional[int] = None,_internal_total_rows: Optional[Union[int, Literal["too_many"]]] = None,) -> None:+ validate_no_integer_columns(data)+# The original data passed inself._data = data# Holds the original datadiff --git a/marimo/_plugins/validators.py b/marimo/_plugins/validators.pyindex ed7a5bc586..935d24b63e 100644--- a/marimo/_plugins/validators.py+++ b/marimo/_plugins/validators.py@@ -4,6 +4,8 @@ from __future__ import annotationsimport warningsfrom typing import Any+from marimo._dependencies.dependencies import DependencyManager+def validate_range(min_value: int | float | None,@@ -57,3 +59,21 @@ def warn_js_safe_number(*values: int | float | None) -> None:"integers in JavaScript. This may cause precision issues.",stacklevel=3,)+++def validate_no_integer_columns(df: Any) -> None:+ if not DependencyManager.pandas.imported():+ return++ import pandas as pd++ if not isinstance(df, pd.DataFrame):+ return++ has_int_column_names = any(isinstance(name, int) for name in df.columns)+ if has_int_column_names:+ warnings.warn(+ "DataFrame has integer column names. This is not supported and can lead to bugs. "+ "Please use strings for column names.",+ stacklevel=3,+ )diff --git a/marimo/_smoke_tests/third_party/pandas/int_column_names.py b/marimo/_smoke_tests/third_party/pandas/int_column_names.pynew file mode 100644index 0000000000..10092a19e5--- /dev/null+++ b/marimo/_smoke_tests/third_party/pandas/int_column_names.py@@ -0,0 +1,34 @@+import marimo++__generated_with = "0.9.27"+app = marimo.App(width="medium")+++def __():+ import marimo as mo+ import pandas as pd+ return mo, pd+++def __(pd):+ pd.DataFrame([1, 2, 3])+ return+++def __(pd):+ data = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=[0, 1, 2])+ data+ return (data,)+++def __(data, mo):+ mo.ui.table(data)+ return+++if __name__ == "__main__":+ app.run()diff --git a/tests/_plugins/ui/_impl/dataframes/test_dataframe.py b/tests/_plugins/ui/_impl/dataframes/test_dataframe.pyindex 2a38b0f5cb..53328eaae4 100644--- a/tests/_plugins/ui/_impl/dataframes/test_dataframe.py+++ b/tests/_plugins/ui/_impl/dataframes/test_dataframe.py@@ -294,3 +294,21 @@ def test_ibis_with_polars_backend() -> None:assert dataframe.get_dataframe(EmptyArgs()).total_rows == 3assert dataframe.get_dataframe(EmptyArgs()).sql_code is Noneibis.set_backend(prev_backend)+++ not DependencyManager.pandas.has(), reason="Pandas not installed"+)+def test_dataframe_with_int_column_names():+ import warnings++ import pandas as pd++ data = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=[0, 1, 2])+ with warnings.catch_warnings(record=True) as w:+ dataframe = ui.dataframe(data)+ # Check that warnings were made+ assert len(w) > 0+ assert "DataFrame has integer column names" in str(w[0].message)++ assert dataframe.value is not Nonediff --git a/tests/_plugins/ui/_impl/test_table.py b/tests/_plugins/ui/_impl/test_table.pyindex e8db85f3a7..ecca88b18c 100644--- a/tests/_plugins/ui/_impl/test_table.py+++ b/tests/_plugins/ui/_impl/test_table.py@@ -850,3 +850,23 @@ def test_column_clamping_with_polars():headers = csv.split("\n")[0].split(",")assert len(headers) == 60 # 60 columnsassert len(table._component_args["field-types"]) == 60+++ not DependencyManager.pandas.has(), reason="Pandas not installed"+)+def test_dataframe_with_int_column_names():+ import warnings++ import pandas as pd++ data = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=[0, 1, 2])+ with warnings.catch_warnings(record=True) as w:+ table = ui.table(data)+ # Check that warnings were made+ assert len(w) > 0+ assert "DataFrame has integer column names" in str(w[0].message)++ # Check that the table handles integer column names correctly+ assert table._manager.get_column_names() == [0, 1, 2]+ assert table._component_args["total-columns"] == 3