Skip to content

fix(core): unnamed-multiindex-roundtrip - #7680

Closed
dvvrtmshra wants to merge 1 commit into
openbq-org:developfrom
dvvrtmshra:hotfix/unnamed-multiindex-roundtrip
Closed

dvvrtmshra wants to merge 1 commit into
openbq-org:developfrom
dvvrtmshra:hotfix/unnamed-multiindex-roundtrip

Conversation

@dvvrtmshra

Copy link
Copy Markdown

Pull Request the OpenBB Platform

Description

  • Summary of the change/ bug fix.

    df_to_basemodel followed by basemodel_to_df raised a KeyError when the DataFrame had an unnamed or partially named MultiIndex. Unnamed levels are stored in multiindex_names as None, but reset_index() turns them into columns named level_0, level_1, etc., so set_index could not find them.

    This PR records the reset-index column names in a new multiindex_columns field, separately from the original level names. basemodel_to_df rebuilds the index from those columns and then restores the original level names (including None). If multiindex_columns is absent, it falls back to the previous behavior, so existing payloads and the fully named case are unaffected.

  • Link # issue, if applicable.

    Fixes #ISSUE_NUMBER

  • Screenshot of the feature or the bug before/after fix, if applicable.

    N/A. Before/after is shown by the reproduction below.

  • Relevant motivation and context.

    Round-tripping a DataFrame with an unnamed MultiIndex, which pandas produces by default from MultiIndex.from_tuples, groupby on multiple keys, etc., failed outright. Fully named MultiIndexes already worked.

  • List any dependencies that are required for this change.

    None.

How has this been tested?

  • Added a parametrized regression test, test_multiindex_round_trip_unnamed_levels, in openbb_platform/core/tests/app/test_utils.py, covering [None, None], ["group", None], and [None, "id"]. Each case asserts the round-tripped DataFrame equals the original via pd.testing.assert_frame_equal.
  • The new tests fail on the current develop code with the reported KeyError and pass with this change. All existing tests in test_utils.py still pass.

Reproduction:

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)]),
)
basemodel_to_df(df_to_basemodel(frame))
# Before: KeyError: 'None of [None, None] are in the columns'
# After: returns the DataFrame with its unnamed MultiIndex restored

Test configuration: Windows, Python 3.11.9, OpenBB installed from source via dev_install.py.

  • Ensure all unit and integration tests pass.
  • If you modified/added command(s):
    • Ensure the command(s) execute with the expected output.
      • API.
      • Python Interface.
    • If applicable, please add new tests for the command (see CONTRIBUTING.md to leverage semi-automated testing).
  • If a new provider was introduced or a new fetcher was added to an existing provider:
    • Ensure the existing tests pass.
    • Ensure the new provider and/or fetcher is stable and usable.
    • If applicable, please add new tests for the provider and/or fetcher (see CONTRIBUTING.md to leverage semi-automated testing).
  • If a new provider or extension was added:

Checklist

  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have adhered to the GitFlow naming convention and my branch name is in the format of feature/feature-name or hotfix/hotfix-name.
  • I ensure that I am following the CONTRIBUTING guidelines.

@CLAassistant

CLAassistant commented Sep 25, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@dlee-bq

dlee-bq commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Hi @dvvrtmshra, thanks for the PR, however this PR is superseded by #7762 because of the V5 file move. Closing as a duplicate.

@dlee-bq dlee-bq closed this Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants