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: resolve duplicate column keys when index name conflicts with column name (#8999)
Shahmir Varqhaa83e8d8af986
diff --git a/marimo/_plugins/ui/_impl/tables/pandas_table.py b/marimo/_plugins/ui/_impl/tables/pandas_table.pyindex fc0449b4cc..340741667b 100644--- a/marimo/_plugins/ui/_impl/tables/pandas_table.py+++ b/marimo/_plugins/ui/_impl/tables/pandas_table.py@@ -42,6 +42,11 @@ def _trivial_range_index(index: pd.Index) -> bool:return isinstance(index, pd.RangeIndex) and index.name is None+def _resolve_index_name(name: object, columns: set[str]) -> str:+ """Return a non-conflicting index name by appending '_index' if needed."""+ return f"{name}_index" if name in columns else str(name)++def _resolve_index_column_conflicts(df: pd.DataFrame) -> pd.DataFrame:"""Rename index names that conflict with column names by appending '_index'.@@ -55,12 +60,8 @@ def _resolve_index_column_conflicts(df: pd.DataFrame) -> pd.DataFrame:if not conflicting_names:return df- new_names: list[str] = []- for name in index_names:- if name in conflicting_names:- new_names.append(f"{name}_index")- else:- new_names.append(str(name))+ columns = set(df.columns)+ new_names = [_resolve_index_name(name, columns) for name in index_names]if isinstance(df.index, pd.MultiIndex):df.index = df.index.set_names(new_names)@@ -329,9 +330,16 @@ class PandasTableManagerFactory(TableManagerFactory):# We override the default implementation to use pandas# headersdef get_row_headers(self) -> FieldTypes:- return self._get_row_headers_for_index(+ headers = self._get_row_headers_for_index(self._original_data.index)+ # Rename headers that collide with column names so the+ # frontend receives names consistent with to_json_str().+ columns = set(self._original_data.columns)+ return [+ (_resolve_index_name(name, columns), ft)+ for name, ft in headers+ ]def _has_non_trivial_index(self) -> bool:"""Check if the DataFrame has a non-trivial index that should be searched."""diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.pyindex e08626aae2..00311a9008 100644--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py@@ -652,6 +652,40 @@ class TestPandasTableManager(unittest.TestCase):[("X", ("string", "str")), ("Y", ("integer", "int64"))],]+ def test_get_row_headers_index_column_name_conflict(self) -> None:+ data = pd.DataFrame(+ {"ID_SOMETHING": [1, 2], "ID_DATE": [3, 4]},+ index=pd.Index(["2026-04-01", "2026-04-02"], name="ID_DATE"),+ )+ manager = self.factory.create()(data)+ headers = manager.get_row_headers()+ # Index renamed to avoid collision with column of the same name+ header_names = [h[0] for h in headers]+ assert header_names == ["ID_DATE_index"]++ def test_get_row_headers_multi_index_partial_conflict(self) -> None:+ data = pd.DataFrame(+ {"x": [1, 2, 3], "y": [4, 5, 6]},+ index=pd.MultiIndex.from_tuples(+ [(1, 4), (2, 5), (3, 6)], names=["x", "z"]+ ),+ )+ manager = self.factory.create()(data)+ headers = manager.get_row_headers()+ # Only the conflicting level is renamed+ header_names = [h[0] for h in headers]+ assert header_names == ["x_index", "z"]++ def test_get_row_headers_no_conflict(self) -> None:+ data = pd.DataFrame(+ {"A": [1, 2, 3]},+ index=pd.Index(["a", "b", "c"], name="idx"),+ )+ manager = self.factory.create()(data)+ headers = manager.get_row_headers()+ header_names = [h[0] for h in headers]+ assert header_names == ["idx"]+def test_is_type(self) -> None:assert self.manager.is_type(self.data)assert not self.manager.is_type("not a dataframe")