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: dont mutate pandas when fixing column types (#6906)

Myles Scolnick884ec62d78e1
diff --git a/marimo/_plugins/ui/_impl/tables/pandas_table.py b/marimo/_plugins/ui/_impl/tables/pandas_table.py
index b8a122226a..001ca57487 100644
--- a/marimo/_plugins/ui/_impl/tables/pandas_table.py
+++ b/marimo/_plugins/ui/_impl/tables/pandas_table.py
@@ -264,9 +264,11 @@ class PandasTableManagerFactory(TableManagerFactory):
if len(data.columns) > 0 and not isinstance(
data.columns[0], str
):
- data.columns = pd.Index(
- [str(name) for name in data.columns]
+ data_copy = data.copy()
+ data_copy.columns = pd.Index(
+ [str(name) for name in data_copy.columns]
)
+ return data_copy
return data
# We override the default implementation to use pandas
diff --git a/marimo/_smoke_tests/pandas/datetime_index_columns.py b/marimo/_smoke_tests/pandas/datetime_index_columns.py
new file mode 100644
index 0000000000..6858cccf39
--- /dev/null
+++ b/marimo/_smoke_tests/pandas/datetime_index_columns.py
@@ -0,0 +1,108 @@
+# /// script
+# requires-python = ">=3.9"
+# dependencies = [
+# "pandas",
+# "openpyxl",
+# ]
+# ///
+
+import marimo
+
+__generated_with = "0.17.0"
+app = marimo.App(width="medium")
+
+
+def _():
+ import marimo as mo
+ return (mo,)
+
+
[email protected](hide_code=True)
+def _(mo):
+ mo.md("""# Issue #6898: DatetimeIndex Lost Across Cells""")
+ return
+
+
+def _():
+ import pandas as pd
+ import io
+
+ # Load COVID-19 time series data from Johns Hopkins GitHub
+ # This dataset has dates as columns, perfect for testing DatetimeIndex columns
+ url = (
+ "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/"
+ "csse_covid_19_data/csse_covid_19_time_series/"
+ "time_series_covid19_confirmed_US.csv"
+ )
+
+ # Read and prepare the data
+ df_raw = pd.read_csv(url)
+
+ # Get only the date columns (skip first 11 metadata columns)
+ date_cols = df_raw.columns[11:]
+
+ # Create a small subset for testing
+ df_subset = df_raw.iloc[:5, 11:20].copy()
+ df_subset
+ return df_subset, pd
+
+
[email protected](hide_code=True)
+def _(mo):
+ mo.md(
+ """
+ ## Test Case 1: DataFrame with DatetimeIndex columns using pipe()
+
+ This reproduces the exact issue from #6898 where `.pipe()` is used
+ to set DatetimeIndex on columns.
+ """
+ )
+ return
+
+
+def _(df_subset, pd):
+ # Reproduce the exact pattern from issue #6898
+ df_with_datetime_cols = df_subset.pipe(
+ lambda d: d.set_axis(pd.DatetimeIndex(pd.to_datetime(d.columns)), axis=1)
+ )
+
+ # In this cell, everything should work correctly
+ print(f"Type in same cell: {type(df_with_datetime_cols.columns)}")
+ print(f"Dtype in same cell: {df_with_datetime_cols.columns.dtype}")
+ print(
+ f"Is DatetimeIndex in same cell: {isinstance(df_with_datetime_cols.columns, pd.DatetimeIndex)}"
+ )
+
+ df_with_datetime_cols
+ return (df_with_datetime_cols,)
+
+
[email protected](hide_code=True)
+def _(mo):
+ mo.md(
+ """
+ ## Test Case 2: Check column type in NEXT cell (reproduces bug)
+
+ This is where the bug manifests - the DatetimeIndex is lost when
+ the DataFrame is passed to the next cell.
+ """
+ )
+ return
+
+
+def _(df_with_datetime_cols, pd):
+ # This should show DatetimeIndex
+ print(f"Type in next cell: {type(df_with_datetime_cols.columns)}")
+ print(f"Dtype in next cell: {df_with_datetime_cols.columns.dtype}")
+ print(
+ f"Is DatetimeIndex in next cell: {isinstance(df_with_datetime_cols.columns, pd.DatetimeIndex)}"
+ )
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/marimo/_smoke_tests/tables/pandas_multi_idx.py b/marimo/_smoke_tests/pandas/pandas_multi_idx.py
similarity index 100%
rename from marimo/_smoke_tests/tables/pandas_multi_idx.py
rename to marimo/_smoke_tests/pandas/pandas_multi_idx.py
diff --git a/marimo/_smoke_tests/tables/pandas_nan.py b/marimo/_smoke_tests/pandas/pandas_nan.py
similarity index 100%
rename from marimo/_smoke_tests/tables/pandas_nan.py
rename to marimo/_smoke_tests/pandas/pandas_nan.py
diff --git a/marimo/_smoke_tests/tables/polars_duration.py b/marimo/_smoke_tests/polars/polars_duration.py
similarity index 100%
rename from marimo/_smoke_tests/tables/polars_duration.py
rename to marimo/_smoke_tests/polars/polars_duration.py
diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.py
index eaa2a892bf..1d48d56314 100644
--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py
+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py
@@ -1274,3 +1274,62 @@ class TestPandasTableManager(unittest.TestCase):
assert "z" in json_data[0] # Non-conflicting index level
assert "x" in json_data[0] # Original column
assert "y" in json_data[0] # Original column
+
+ def test_handle_non_string_column_names_no_mutation(self) -> None:
+ import pandas as pd
+
+ df = pd.DataFrame(
+ [[1, 2, 3], [4, 5, 6]],
+ columns=pd.DatetimeIndex(
+ ["2021-01-01", "2021-01-02", "2021-01-03"]
+ ),
+ )
+
+ # Store the original column types
+ original_columns = df.columns
+ original_columns_type = type(df.columns)
+ assert isinstance(original_columns, pd.DatetimeIndex)
+
+ # Create table manager (which calls _handle_non_string_column_names)
+ manager = self.factory.create()(df)
+
+ # Verify the manager has string column names
+ column_names = manager.get_column_names()
+ assert all(isinstance(name, str) for name in column_names)
+ assert column_names == [
+ "2021-01-01 00:00:00",
+ "2021-01-02 00:00:00",
+ "2021-01-03 00:00:00",
+ ]
+
+ # Verify the original DataFrame was NOT mutated
+ assert isinstance(df.columns, original_columns_type)
+ assert isinstance(df.columns, pd.DatetimeIndex)
+ assert all(isinstance(col, pd.Timestamp) for col in df.columns), (
+ "Original DataFrame columns should still be Timestamps"
+ )
+
+ def test_handle_integer_column_names_no_mutation(self) -> None:
+ import pandas as pd
+
+ # Test with integer column names
+ df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=[0, 1, 2])
+
+ # Store the original column info
+ original_columns_type = type(df.columns[0])
+ # Note: pandas converts int to np.int64
+ assert not isinstance(df.columns[0], str)
+
+ # Create table manager
+ manager = self.factory.create()(df)
+
+ # Verify the manager has string column names
+ column_names = manager.get_column_names()
+ assert all(isinstance(name, str) for name in column_names)
+ assert column_names == ["0", "1", "2"]
+
+ # Verify the original DataFrame was NOT mutated
+ assert isinstance(df.columns[0], original_columns_type)
+ assert not any(isinstance(col, str) for col in df.columns), (
+ "Original DataFrame columns should NOT be strings"
+ )