Repository navigation
fix(converters): preserve literal NA strings in XLSXToDocument (#12945) #13044
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
Open
HarshitR2004
wants to merge
9
commits into
deepset-ai:main
Choose a base branch
from
HarshitR2004:fix-xlsx-drops-literal-na-values
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+225
−1
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e73435f
fix(converters): preserve literal NA strings in XLSXToDocument (#12945)
HarshitR2004 4aa9424
Merge branch 'main' into fix-xlsx-drops-literal-na-values
HarshitR2004 b75c940
fix(converters): retain empty cells with custom XLSX NA values
HarshitR2004 bd8af30
Merge branch 'main' into fix-xlsx-drops-literal-na-values
HarshitR2004 16349a4
Merge branch 'main' into fix-xlsx-drops-literal-na-values
davidsbatista a43932d
Merge branch 'main' into fix-xlsx-drops-literal-na-values
davidsbatista 4cf7876
fix(converters): preserve explicit XLSX NA parsing settings
HarshitR2004 7ad32c7
Apply suggestion from @davidsbatista
davidsbatista 6813d85
Merge branch 'main' into fix-xlsx-drops-literal-na-values
HarshitR2004 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
15 changes: 15 additions & 0 deletions
15
releasenotes/notes/fix-xlsx-drops-literal-na-values-b18412ebd88214fe.yaml
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| --- | ||
| fixes: | ||
| - | | ||
| Fixed an issue where ``XLSXToDocument`` dropped literal text values such as ``"NA"`` and ``"N/A"``, | ||
| converting them into empty cells. When neither ``keep_default_na`` nor ``na_values`` is supplied in | ||
| ``read_excel_kwargs``, the converter uses ``keep_default_na=False`` and ``na_values=[""]`` to preserve | ||
| literal text while still recognizing empty cells as missing. | ||
| upgrade: | ||
| - | | ||
| If you do not configure ``keep_default_na`` or ``na_values``, ``XLSXToDocument`` now preserves strings | ||
| such as ``"NA"``, ``"N/A"``, ``"NULL"``, and ``"None"`` instead of treating them as missing values. | ||
| Pass ``read_excel_kwargs={"keep_default_na": True}`` to restore pandas' previous default NA parsing. | ||
| If either option is already supplied, the converter preserves pandas' existing behavior: custom | ||
| ``na_values`` alone supplement the built-in NA strings, while explicit ``keep_default_na=False`` | ||
| disables those strings without adding ``""`` to the missing values or changing numeric formatting. | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,9 @@ | |
| import csv | ||
| import io | ||
| import logging | ||
| from copy import deepcopy | ||
| from pathlib import Path | ||
| from typing import Literal | ||
| from typing import Any, Literal | ||
|
|
||
| import pytest | ||
| from openpyxl import Workbook | ||
|
|
@@ -254,3 +255,195 @@ def test_link_extraction_from_typed_columns( | |
| ] | ||
| assert rows == [linked_values + ["1.23"], ["43", "4.5", "False", "2.35"]] | ||
| assert documents[0].meta["xlsx"] == {"sheet_name": "Sheet"} | ||
|
|
||
| @pytest.mark.parametrize("table_format", ["csv", "markdown"]) | ||
| @pytest.mark.parametrize("read_excel_kwargs", [{}, {"skiprows": 0}], ids=["defaults", "unrelated-option"]) | ||
| @pytest.mark.parametrize("missing_value", ["", "<missing>"]) | ||
| def test_run_preserves_literal_na_strings( | ||
| self, | ||
| tmp_path: Path, | ||
| table_format: Literal["csv", "markdown"], | ||
| read_excel_kwargs: dict[str, Any], | ||
| missing_value: str, | ||
| ) -> None: | ||
| workbook = Workbook() | ||
| sheet = workbook.active | ||
| assert sheet is not None | ||
| sheet.append(["Code", "Status", "Blank"]) | ||
| sheet.append(["NA", "OK", None]) | ||
| sheet.append(["N/A", "pending", ""]) | ||
| sheet.append(["NULL", "None", None]) | ||
| sheet.append(["NaN", "null", ""]) | ||
| sheet.append(["North America", "active", ""]) | ||
| path = tmp_path / "literal_na.xlsx" | ||
| workbook.save(path) | ||
| workbook.close() | ||
|
|
||
| original_kwargs = deepcopy(read_excel_kwargs) | ||
| format_kwargs = {"na_rep": missing_value} if table_format == "csv" else {"missingval": missing_value} | ||
| converter = XLSXToDocument( | ||
| table_format=table_format, read_excel_kwargs=read_excel_kwargs, table_format_kwargs=format_kwargs | ||
| ) | ||
| documents = converter.run(sources=[path])["documents"] | ||
|
|
||
| assert len(documents) == 1 | ||
| content = documents[0].content | ||
| assert content is not None | ||
|
|
||
| if table_format == "csv": | ||
| rows = [row[1:] for row in list(csv.reader(io.StringIO(content)))[1:]] | ||
| else: | ||
| rows = [[cell.strip() for cell in row.split("|")[2:-1]] for row in content.splitlines()[2:]] | ||
| assert rows == [ | ||
| ["Code", "Status", "Blank"], | ||
| ["NA", "OK", missing_value], | ||
| ["N/A", "pending", missing_value], | ||
| ["NULL", "None", missing_value], | ||
| ["NaN", "null", missing_value], | ||
| ["North America", "active", missing_value], | ||
| ] | ||
| assert read_excel_kwargs == original_kwargs | ||
|
|
||
| @pytest.mark.parametrize("table_format", ["csv", "markdown"]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you parametrize this test over a tuple, a dict and None for na_values as well? Those are the cases that break right now. |
||
| @pytest.mark.parametrize( | ||
| ("na_values", "expected_custom_values"), | ||
| [ | ||
| pytest.param(["foo"], ["<missing>", "<missing>"], id="list"), | ||
| pytest.param(["foo", ""], ["<missing>", "<missing>"], id="list-with-empty-string"), | ||
| pytest.param([], ["foo", "foo"], id="empty-list"), | ||
| pytest.param(("foo",), ["<missing>", "<missing>"], id="tuple"), | ||
| pytest.param({"foo"}, ["<missing>", "<missing>"], id="set"), | ||
| pytest.param("foo", ["<missing>", "<missing>"], id="string"), | ||
| pytest.param({0: ["foo"]}, ["<missing>", "foo"], id="column-dict"), | ||
| pytest.param(None, ["foo", "foo"], id="none"), | ||
| ], | ||
| ) | ||
| def test_run_custom_na_values_preserves_empty_cells( | ||
| self, | ||
| tmp_path: Path, | ||
| table_format: Literal["csv", "markdown"], | ||
| na_values: Any, | ||
| expected_custom_values: list[str], | ||
| ) -> None: | ||
| workbook = Workbook() | ||
| sheet = workbook.active | ||
| assert sheet is not None | ||
| sheet.append(["Code", "Status", "Blank"]) | ||
| sheet.append(["foo", "foo", None]) | ||
| sheet.append(["NA", "N/A", "value"]) | ||
| sheet.append(["NULL", "None", ""]) | ||
| path = tmp_path / "custom_na.xlsx" | ||
| workbook.save(path) | ||
| workbook.close() | ||
|
|
||
| read_excel_kwargs = {"na_values": na_values} | ||
| original_kwargs = deepcopy(read_excel_kwargs) | ||
| format_kwargs = {"na_rep": "<missing>"} if table_format == "csv" else {"missingval": "<missing>"} | ||
| converter = XLSXToDocument( | ||
| table_format=table_format, read_excel_kwargs=read_excel_kwargs, table_format_kwargs=format_kwargs | ||
| ) | ||
| documents = converter.run(sources=[path])["documents"] | ||
|
|
||
| assert len(documents) == 1 | ||
| content = documents[0].content | ||
| assert content is not None | ||
| if table_format == "csv": | ||
| rows = [row[1:] for row in list(csv.reader(io.StringIO(content)))[1:]] | ||
| else: | ||
| rows = [[cell.strip() for cell in row.split("|")[2:-1]] for row in content.splitlines()[2:]] | ||
| assert rows == [ | ||
| ["Code", "Status", "Blank"], | ||
| [*expected_custom_values, "<missing>"], | ||
| ["<missing>", "<missing>", "value"], | ||
| ["<missing>", "<missing>", "<missing>"], | ||
| ] | ||
| assert read_excel_kwargs == original_kwargs | ||
|
|
||
| @pytest.mark.parametrize("table_format", ["csv", "markdown"]) | ||
| @pytest.mark.parametrize( | ||
| ("read_excel_kwargs", "expected_rows"), | ||
| [ | ||
| pytest.param( | ||
| {"keep_default_na": True}, | ||
| [["foo", "<missing>", "<missing>"], ["<missing>", "<missing>", "value"]], | ||
| id="keep-default-na-true", | ||
| ), | ||
| pytest.param( | ||
| {"keep_default_na": True, "na_values": ["foo"]}, | ||
| [["<missing>", "<missing>", "<missing>"], ["<missing>", "<missing>", "value"]], | ||
| id="keep-default-na-true-with-custom-values", | ||
| ), | ||
| pytest.param( | ||
| {"keep_default_na": False}, [["foo", "NA", ""], ["N/A", "", "value"]], id="keep-default-na-false" | ||
| ), | ||
| pytest.param( | ||
| {"keep_default_na": False, "na_values": ["foo"]}, | ||
| [["<missing>", "NA", ""], ["N/A", "", "value"]], | ||
| id="keep-default-na-false-with-custom-values", | ||
| ), | ||
| pytest.param( | ||
| {"keep_default_na": False, "na_values": ["foo", ""]}, | ||
| [["<missing>", "NA", "<missing>"], ["N/A", "<missing>", "value"]], | ||
| id="explicit-empty-string", | ||
| ), | ||
| pytest.param({"na_filter": False}, [["foo", "NA", ""], ["N/A", "", "value"]], id="na-filter-false"), | ||
| ], | ||
| ) | ||
| def test_run_na_parsing_overrides( | ||
| self, | ||
| tmp_path: Path, | ||
| table_format: Literal["csv", "markdown"], | ||
| read_excel_kwargs: dict[str, Any], | ||
| expected_rows: list[list[str]], | ||
| ) -> None: | ||
| workbook = Workbook() | ||
| sheet = workbook.active | ||
| assert sheet is not None | ||
| sheet.append(["Code", "Status", "Blank"]) | ||
| sheet.append(["foo", "NA", None]) | ||
| sheet.append(["N/A", "", "value"]) | ||
| path = tmp_path / "override_na.xlsx" | ||
| workbook.save(path) | ||
| workbook.close() | ||
|
|
||
| original_kwargs = deepcopy(read_excel_kwargs) | ||
| format_kwargs = {"na_rep": "<missing>"} if table_format == "csv" else {"missingval": "<missing>"} | ||
| converter = XLSXToDocument( | ||
| table_format=table_format, read_excel_kwargs=read_excel_kwargs, table_format_kwargs=format_kwargs | ||
| ) | ||
| documents = converter.run(sources=[path])["documents"] | ||
|
|
||
| assert len(documents) == 1 | ||
| content = documents[0].content | ||
| assert content is not None | ||
| if table_format == "csv": | ||
| rows = [row[1:] for row in list(csv.reader(io.StringIO(content)))[1:]] | ||
| else: | ||
| rows = [[cell.strip() for cell in row.split("|")[2:-1]] for row in content.splitlines()[2:]] | ||
| assert rows == [["Code", "Status", "Blank"], *expected_rows] | ||
| assert read_excel_kwargs == original_kwargs | ||
|
|
||
| @pytest.mark.parametrize("na_values", [None, ["foo"]], ids=["no-custom-values", "custom-values"]) | ||
| def test_run_keep_default_na_false_preserves_numeric_formatting(self, tmp_path: Path, na_values: Any) -> None: | ||
| workbook = Workbook() | ||
| sheet = workbook.active | ||
| assert sheet is not None | ||
| sheet.append([1, "x"]) | ||
| sheet.append([None, "foo"]) | ||
| sheet.append([2, "y"]) | ||
| path = tmp_path / "numeric_gaps.xlsx" | ||
| workbook.save(path) | ||
| workbook.close() | ||
|
|
||
| read_excel_kwargs: dict[str, Any] = {"keep_default_na": False} | ||
| if na_values is not None: | ||
| read_excel_kwargs["na_values"] = na_values | ||
| converter = XLSXToDocument(read_excel_kwargs=read_excel_kwargs, table_format_kwargs={"na_rep": "<missing>"}) | ||
| documents = converter.run(sources=[path])["documents"] | ||
|
|
||
| assert len(documents) == 1 | ||
| content = documents[0].content | ||
| assert content is not None | ||
| rows = [row[1:] for row in list(csv.reader(io.StringIO(content)))[1:]] | ||
| expected_foo = "foo" if na_values is None else "<missing>" | ||
| assert rows == [["1", "x"], ["", expected_foo], ["2", "y"]] | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes the output for everyone using the default settings, e.g. cells containing
"NULL"or"None"now end up in the content instead of being emptied. Let's add anupgradesection explaining how to get the old behavior back.