Skip to content

Fix to_excel not rendering None values in MultiIndex #51824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

ghost
Copy link

@ghost ghost commented Mar 7, 2023


The change fixes the issue of not rendering NaN-valued column cells when exported to Excel.

import pandas as pd
import numpy as np

Cols = pd.MultiIndex.from_tuples(
    [
        ("Item", "Type 1", "Class A"),
        ("Item", "Type 1", "Class B"),
        ("Item", "Type 2", "Class A"),
        ("Item", "Type 2", "Class B"),
        ("Item", "Type 3", None),
    ]
)

df = pd.DataFrame(np.random.randn(10, 5), columns=Cols)
df.to_excel("Test.xlsx")
df

However, when I do roundtripping of the Excel sheet back into a DataFrame I get this:

pd.read_excel("Test.xlsx", header=[0, 1, 2], index_col=[0])

So I have a question. Should I update the logic of read_excel to also apply the conversion of NaN values of regular cells (specified by the na_values parameter) to column values as well? If so, would it make more sense to do it here (where regular values are converted), or here (where column values assigned the Unnamed values)? Or there's some reason why the current functionality is not applied for column values and the user is expected to convert them manually (like here)?

@ghost ghost changed the title Fix to_excel not respecting None values in MultiIndex Fix to_excel not rendering None values in MultiIndex Mar 8, 2023
@rhshadrach
Copy link
Member

I don't believe we can differentiate between '' and an NA value in the Excel file. Would it make more sense to assume the value is '' instead of an NA value?

@mroeschke mroeschke added the IO Excel read_excel, to_excel label Mar 10, 2023
@mroeschke
Copy link
Member

Thanks for the pull request, but it appears to have gone stale. If interested in continuing, please merge in the main branch, address any review comments and/or failing tests, and we can reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO Excel read_excel, to_excel
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: to_excel inserts invalid data if a level in a MultiIndex is None
3 participants