[Bug] DataFrame round-trip fails for unnamed MultiIndex levels
Describe the bug
df_to_basemodel followed by basemodel_to_df raises KeyError when the input DataFrame has an unnamed or partially named pandas MultiIndex.
The conversion stores df.index.names as multiindex_names. For unnamed levels, pandas reports None; the metadata becomes [None, None] (or contains None for a partially named index). basemodel_to_df then passes those values to DataFrame.set_index, even though df_to_basemodel reset the levels into columns named level_0, level_1, etc.
To Reproduce
Run this in an environment with the OpenBB Platform source installed and pandas available:
import pandas as pd
from openbb_core.app.utils import basemodel_to_df, df_to_basemodel
frame = pd.DataFrame(
{"value": [10, 20]},
index=pd.MultiIndex.from_tuples([("A", 1), ("B", 2)]),
)
models = df_to_basemodel(frame)
basemodel_to_df(models)Actual behavior
The call raises:
KeyError: 'None of [None, None] are in the columns'The first converted record contains level_0, level_1, value, and multiindex_names: '[None, None]'. A partially named index, such as names=['group', None], fails similarly. A fully named MultiIndex round-trips successfully.
Expected behavior
The conversion should preserve the MultiIndex values and return a DataFrame with an unnamed (or partially named) MultiIndex, without raising. The reset-index column names could be recorded separately from the original index-level names so unnamed levels can be restored correctly.
Environment and source
- OpenBB repository commit:
3e071fcc2cd9f891cac6040ae60296dba76dab46 - Python:
3.12.13 - pandas:
3.0.6 - Reproduced using
openbb_platform/core/openbb_core/app/utils.pyfrom this checkout.
Relevant code: df_to_basemodel records index names and resets the index at openbb_platform/core/openbb_core/app/utils.py lines 75–79; basemodel_to_df passes the stored values to set_index at lines 40–43. Existing tests cover a named MultiIndex, but not unnamed levels, in openbb_platform/core/tests/app/test_utils.py.
Source: OpenBB-finance/OpenBB