Skip to content

fix(xlsx): keep whole numbers and booleans in a column with an empty cell - #4556

Open
L4XB wants to merge 5 commits into
Unstructured-IO:mainfrom
L4XB:fix/xlsx-cells-as-written
Open

L4XB wants to merge 5 commits into
Unstructured-IO:mainfrom
L4XB:fix/xlsx-cells-as-written

Conversation

@L4XB

@L4XB L4XB commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

partition_xlsx() reads each worksheet with pd.read_excel() and its default dtype inference, then writes the frame with to_html(na_rep=""). Pandas gives a column that holds a missing value a float dtype. One empty cell therefore turns a column of whole numbers into floats, and a column of booleans into 1.0 / 0.0:

cells element text on 0.27.28
12, (empty), 7, 3 12.0, 7.0, 3.0
TRUE, FALSE, (empty) 1.0, 0.0

A probe sheet ([12, "pen", True], [None, "ink", False], [7, "pad", None], [3, "007", 1.5]) comes out of main as 12.0 pen 1.0 ink 0.0 7.0 pad 3.0 007 1.5. With this PR it reads 12 pen True ink False 7 pad 3 007 1.5.

Change

Both read paths, the plain pd.read_excel() and the row-compacting reader used for subtable detection, now pass dtype=object (_CELLS_AS_WRITTEN). Each cell keeps the value openpyxl or xlrd gives for it. Empty cells are still missing, so subtable detection sees the same populated cells as before.

Related: #4505 reads cell text such as N/A, NULL or None as content (keep_default_na=False). I first had that here too, then left it out so the two PRs do not overlap. Each one only adds read options, so they combine without trouble.

Out of scope: a date cell still shows its time (2024-09-30 00:00:00).

Testing

  • New test_partition_xlsx_reads_each_cell_as_written, parametrized over find_subtable, checks the text and text_as_html of the probe sheet. Both cases fail on main.
  • The other 142 tests in test_xlsx.py pass unchanged, including the .xls ones.
  • ruff 0.15.10 check and format are clean, and scripts/version-sync.sh -c passes with 0.27.34-dev7.

L4XB added 2 commits October 17, 2026 16:18
partition_xlsx() read each worksheet with Pandas' dtype inference. One
empty cell turned a column of whole numbers into floats, so 12 came out
as 12.0, and a column of booleans into 1.0 and 0.0. Pandas' default
missing-value markers also read the text N/A, NULL, NA or None in a cell
as missing, so it was dropped from the element.

Worksheets are now read with dtype=object and keep_default_na=False, with
only an empty cell as missing, on both the plain and the row-compacting
reader. Subtable detection still sees empty cells as empty.
Unstructured-IO#4505 already reads cell text such as N/A, NULL or None as content. This
PR now only stops Pandas from inferring a dtype per column, so whole
numbers and booleans in a column with an empty cell keep their values;
the test uses a sheet without N/A-like text.
@L4XB L4XB changed the title fix(xlsx): read spreadsheet cells as the workbook holds them fix(xlsx): keep whole numbers and booleans in a column with an empty cell Oct 17, 2026
Version 0.27.34 with this branch's changelog section on top. main reads
worksheets through ExcelWorkbookReader.parse now (Unstructured-IO#4562, Unstructured-IO#4563), so the
dtype=object read moves there; it reaches partition_xlsx and
open_excel_workbook() callers alike.
@L4XB

L4XB commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

I merged main (29f4d86) and bumped the version to 0.27.34. Since #4562 and #4563, worksheets are read through ExcelWorkbookReader.parse(), so dtype=object now goes there. It reaches partition_xlsx() and open_excel_workbook() callers alike, which matches the reader's purpose of serving integrations the same cell values the partitioner sees. test_xlsx.py: 159 passed. test_partition_xlsx_reads_each_cell_as_written fails against main's xlsx.py.

…tten

# Conflicts:
#	CHANGELOG.md
#	unstructured/__version__.py

@cragwolfe cragwolfe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3a3bea159531463c4a1f6f409a90545868116a24 with GPT Pro.

P1 — Object dtype makes small nonzero numeric cells serialize as zero (unstructured/partition/xlsx.py:197, interacting with _TableRowRenderer at 147–162). The reader preserves raw cell values, but the unchanged DataFrameFormatter chooses formatting by dtype. For object columns, Pandas’ generic formatter uses fixed-point display precision (six decimal places by default); the former float-array formatter switches small values to scientific notation.

A native numeric worksheet with rows [101, 1e-7] and [102, 2e-7] therefore changes from nonzero scientific-notation readings to 0.0 for both readings. This affects core tables under either find_subtable setting, header-enabled tables with a real header, and text output even when HTML is disabled. It is silent numeric corruption in previously numeric columns, not merely a display-style change. The new test’s 1.5 value does not catch it.

Keep the intended integer/boolean/blank preservation, and give the row renderer a local explicit float-formatting policy that preserves small magnitudes, signs and meaningful precision. Avoid changing global Pandas settings. Add serialized-workbook regressions with positive/negative small floats, zero, ordinary fractions and the intended integer/boolean cases; assert concrete text/HTML across subtable/header modes and equal text with HTML disabled. Cover the shared .xls reader too.

The merged #4562/#4563 reader and formatter changes are present in this head. Their geometry, guard and cleanup logic is unchanged. #4505’s separate NA-string options are absent and would not repair this formatting regression; its old NA-string loss is not an additional finding against this PR.

Required CI remains unverified: workflow runs require action and external successes do not establish test execution. No local tests or reproductions were run.

(authored by codex)

cragwolfe pushed a commit that referenced this pull request Oct 22, 2026
## Summary

`read_delimited_text()`, which reads every CSV and TSV upload, calls
`pd.read_csv()` with Pandas' default cell interpretation. That changes
what the file says in three ways:

| cell | `main` | this PR |
|---|---|---|
| `N/A`, `NULL`, `None` (and `NA`, `nan`, `n/a`, …) | empty | `N/A`,
`NULL`, `None` |
| `12` in a column that has an empty cell, with `include_header=True` |
`12.0` | `12` |
| `007` in a column of codes that has an empty cell, with
`include_header=True` | `7.0` | `007` |

Probe file `name,qty,status,code` / `Pen,12,N/A,007` / `Ink,,NULL,010` /
`Pad,7,ok,`. On `main` with `include_header=True` it reads `name qty
status code Pen 12.0 7.0 Ink 10.0 Pad 7.0 ok`, and with
`include_header=False` it reads `name qty status code Pen 12 007 Ink 010
Pad 7 ok`. With this PR it reads `name qty status code Pen 12 N/A 007
Ink NULL 010 Pad 7 ok`. Without a header row, the header strings already
make each column `object`, so only the `N/A` / `NULL` loss shows there.

## Change

Both `pd.read_csv()` calls in `read_delimited_text()` pass `dtype=str,
keep_default_na=False`. Every cell is the text the file holds, and an
empty field is an empty string. The HTML table still writes an empty
cell as empty. Delimiter sniffing, the lone-`\r` reader and the
cell-count limit are unchanged.

Related: #4505 does the same for XLSX (`keep_default_na=False`), and
#4556 keeps XLSX whole numbers (`dtype=object`). Each PR only adds read
options to its own reader.

## Testing

- New `test_partition_csv_reads_each_cell_as_written` (both
`include_header` values) and
`test_partition_tsv_reads_each_cell_as_written` fail on `main`.
- The rest of `test_csv.py` and `test_tsv.py` passes (97 in total).
- The 12 CSV/TSV files in `example-docs` partition to exactly the same
element text and HTML as on `main`.
- ruff 0.15.10 check and format are clean, and `scripts/version-sync.sh
-c` passes with `0.27.34-dev1`.



<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/Unstructured-IO/unstructured/pull/4574?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="View guided diff"
src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
…tten

# Conflicts:
#	CHANGELOG.md
#	unstructured/__version__.py

This branch has not been deployed

No deployments
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.

2 participants