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: pandas dataframe named index cells showing empty (#7942) (#7944)

Myles Scolnick3127f40209e4
diff --git a/marimo/_plugins/ui/_impl/tables/pandas_table.py b/marimo/_plugins/ui/_impl/tables/pandas_table.py
index 1af1624e84..d19b6171e9 100644
--- a/marimo/_plugins/ui/_impl/tables/pandas_table.py
+++ b/marimo/_plugins/ui/_impl/tables/pandas_table.py
@@ -151,9 +151,15 @@ class PandasTableManagerFactory(TableManagerFactory):
)
# Flatten row multi-index
+ # Reset index if it's a MultiIndex or a named Index
+ # (including named RangeIndex, which pandas 3.0 uses for sequential integers)
+ # Only skip reset for unnamed default RangeIndex (0, 1, 2, ...)
if isinstance(result.index, pd.MultiIndex) or (
isinstance(result.index, pd.Index)
- and not isinstance(result.index, pd.RangeIndex)
+ and not (
+ isinstance(result.index, pd.RangeIndex)
+ and result.index.name is None
+ )
):
index_names = result.index.names
unnamed_indexes = any(
diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.py
index c092d50a2f..38c578c6a0 100644
--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py
+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py
@@ -358,6 +358,33 @@ class TestPandasTableManager(unittest.TestCase):
{"index": "e", "a": 3},
]
+ def test_to_json_named_range_index(self) -> None:
+ # Test for issue #7942: Named RangeIndex (pandas 3.0 behavior)
+ # When setting a sequential integer column as index, pandas 3.0
+ # creates a RangeIndex with a name instead of Int64Index
+ x = [10, 20, -10, 20, 10]
+ yr = list(range(2015, 2020))
+ data = pd.DataFrame({"Year": yr, "Observation": x})
+ data.set_index("Year", inplace=True)
+
+ # Verify the index is a RangeIndex with a name (pandas 3.0+)
+ # In pandas 2.x, this would be Int64Index, but both should work
+ assert data.index.name == "Year"
+
+ # The JSON should include the Year values
+ json_data = self.factory_create_json_from_df(data)
+ assert json_data == [
+ {"Year": 2015, "Observation": 10},
+ {"Year": 2016, "Observation": 20},
+ {"Year": 2017, "Observation": -10},
+ {"Year": 2018, "Observation": 20},
+ {"Year": 2019, "Observation": 10},
+ ]
+
+ # Also verify row_headers are correct
+ manager = self.factory.create()(data)
+ assert manager.get_row_headers() == [("Year", ("integer", "int64"))]
+
def test_to_json_multi_index(self) -> None:
# Named index
data = pd.DataFrame(