From 06d42ab0b3671ec9c064e322581081e51effcfa4 Mon Sep 17 00:00:00 2001 From: Stefan Siegel Date: Thu, 7 Nov 2024 15:46:54 +0100 Subject: [PATCH] fix: Convert MultiIndex columns to string representation Previously, report generation failed for DataFrames with a MultiIndex on the column axis due to a "TypeError: Setting a MultiIndex dtype to anything other than object is not supported." This update converts all indexes to their string representation: simple indexes remain unchanged, while MultiIndexes are represented in a tuple-style format. --- .../model/pandas/dataframe_pandas.py | 2 +- tests/unit/test_multiindex_columns.py | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_multiindex_columns.py diff --git a/src/ydata_profiling/model/pandas/dataframe_pandas.py b/src/ydata_profiling/model/pandas/dataframe_pandas.py index 2a6ebbbab..bb5be516b 100644 --- a/src/ydata_profiling/model/pandas/dataframe_pandas.py +++ b/src/ydata_profiling/model/pandas/dataframe_pandas.py @@ -32,5 +32,5 @@ def pandas_preprocess(config: Settings, df: pd.DataFrame) -> pd.DataFrame: df = rename_index(df) # Ensure that columns are strings - df.columns = df.columns.astype("str") + df.columns = df.columns.map(str) return df diff --git a/tests/unit/test_multiindex_columns.py b/tests/unit/test_multiindex_columns.py new file mode 100644 index 000000000..3d9558968 --- /dev/null +++ b/tests/unit/test_multiindex_columns.py @@ -0,0 +1,20 @@ +import pandas as pd +import pytest + +from ydata_profiling import ProfileReport + + +@pytest.fixture() +def df(): + df = pd.DataFrame( + { + ("foo", "one"): [1, 2, 3], + ("bar", "two"): [4, 5, 6], + } + ) + return df + + +def test_multiindex_columns(df: pd.DataFrame): + profile_report = ProfileReport(df, title="Test Report", progress_bar=False) + assert len(profile_report.to_html()) > 0