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: handle duplicate columns in pandas when inferring datatype (#1313)

Myles Scolnickc82aec807914
diff --git a/marimo/_plugins/ui/_impl/tables/pandas_table.py b/marimo/_plugins/ui/_impl/tables/pandas_table.py
index 84ac0d22f5..4642c0474c 100644
--- a/marimo/_plugins/ui/_impl/tables/pandas_table.py
+++ b/marimo/_plugins/ui/_impl/tables/pandas_table.py
@@ -115,8 +115,16 @@ class PandasTableManagerFactory(TableManagerFactory):
}
@staticmethod
- def _get_field_type(series: pd.Series[Any]) -> FieldType:
- dtype = str(series.dtype)
+ def _get_field_type(
+ series: pd.Series[Any] | pd.DataFrame,
+ ) -> FieldType:
+ # If a df has duplicate columns, it won't be a series, but
+ # a dataframe. In this case, we take the dtype of the columns
+ if isinstance(series, pd.DataFrame):
+ dtype = str(series.columns.dtype)
+ else:
+ dtype = str(series.dtype)
+
if dtype.startswith("interval"):
return "string"
if dtype.startswith("int") or dtype.startswith("uint"):
diff --git a/marimo/_smoke_tests/bugs/1312.py b/marimo/_smoke_tests/bugs/1312.py
new file mode 100644
index 0000000000..80f6ed973c
--- /dev/null
+++ b/marimo/_smoke_tests/bugs/1312.py
@@ -0,0 +1,29 @@
+import marimo
+
+__generated_with = "0.4.11"
+app = marimo.App()
+
+
+def __(pd):
+ df = pd.DataFrame({"a": [1, 2, 3], "b": [1, 2, 3], "c": [1, 2, 3]})
+ renamed = df.rename({"b": "a"}, axis=1)
+ renamed
+ return df, renamed
+
+
+def __(mo, renamed):
+ mo.ui.table(renamed)
+ return
+
+
+def __():
+ import marimo as mo
+ import pandas as pd
+ return mo, pd
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.py
index fef2b48fb1..113c295c6a 100644
--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py
+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py
@@ -157,3 +157,54 @@ class TestPandasTableManager(unittest.TestCase):
self.factory.create()(complex_data).get_field_types()
== expected_field_types
)
+
+ def test_get_fields_types_duplicate_columns(self) -> None:
+ import pandas as pd
+
+ # Different types
+ data = pd.DataFrame(
+ {
+ "A": [1, 2, 3],
+ "B": ["a", "b", "c"],
+ }
+ )
+ data = data.rename(columns={"A": "B"})
+ expected_field_types = {
+ "B": "string",
+ }
+ assert (
+ self.factory.create()(data).get_field_types()
+ == expected_field_types
+ )
+
+ # Both strings
+ data = pd.DataFrame(
+ {
+ "A": ["a", "b", "c"],
+ "B": ["d", "e", "f"],
+ }
+ )
+ data = data.rename(columns={"A": "B"})
+ expected_field_types = {
+ "B": "string",
+ }
+ assert (
+ self.factory.create()(data).get_field_types()
+ == expected_field_types
+ )
+
+ # Both integers
+ data = pd.DataFrame(
+ {
+ "A": [1, 2, 3],
+ "B": [4, 5, 6],
+ }
+ )
+ data = data.rename(columns={"A": "B"})
+ expected_field_types = {
+ "B": "string",
+ }
+ assert (
+ self.factory.create()(data).get_field_types()
+ == expected_field_types
+ )