fix(xlsx): preserve blank cells and literal NA-like strings - #2535
Open
shiyuanyeming-hub wants to merge 1 commit into
Open
shiyuanyeming-hub wants to merge 1 commit into
shiyuanyeming-hub wants to merge 1 commit into
Conversation
pd.read_excel applies pandas' default missing-value handling, which rewrites the sheet contents in two ways: literal strings such as "NA", "NULL" or "None" are read as missing values, and a column that contains a missing value is promoted to float64, so whole numbers render as "2.0" and blank cells render as the text "NaN". Pass keep_default_na=False to the openpyxl and the xlrd readers so the converter reproduces the values that are actually stored in the sheet. Fixes microsoft#2484 Fixes microsoft#2498
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pd.read_excelapplies pandas' default missing-value handling, so the values that reach the Markdown renderer are not the values stored in the sheet:NA,NULLorNoneare read as missing values, andfloat64, so whole numbers render as2.0and blank cells render as the textNaN.Both the
.xlsxand the.xlsreaders now passkeep_default_na=False, so the converter reproduces what is actually stored in the file.Fixes #2484
Fixes #2498
Before / after
tests/test_files/test_na_values.xls, added in this PR.before
after
Why
keep_default_na=Falseand notdtype=objectdtype=objectalso fixes #2484, but it is not safe:keep_default_na=Falsedtype=object2024-01-052024-01-052024-01-052024-01-05 00:00:001.23456789012345e-71.234568e-071.234568e-070.02in a column with a blank2.022NANaNNANaNdtype=objectappends a time component to dates and collapses small floats to0.0, and it does not fix #2498 at all.Tests
New file
packages/markitdown/tests/test_excel_na_values.py:NA/NULL/None/n/a/null/nansurvive verbatim (Excel conversion replaces literal NA/NULL strings and blank cells with NaN #2498)NaN(Excel conversion replaces literal NA/NULL strings and blank cells with NaN #2498).xls(xlrd) reader behaves the same way, using the newtests/test_files/test_na_values.xlsfixturedtype=objectalternative aboveI checked that 9 of the 10 tests fail on the current
main, and that the tenth fails if the fix is changed todtype=object.Full suite locally:
1004 passed, 14 skipped. The one unrelated failure,test_speech_transcription, is a missingffprobebinary on this machine.black --checkpasses.Note on the behaviour change
This changes the default output for sheets containing blank cells or literal
NA-like strings, which is what both issues ask for. If you would rather keep the current default and make it opt-in, I am happy to rework it.