From c60394186d7e5f794491bad3971584a838617af5 Mon Sep 17 00:00:00 2001 From: Lee Sang Gyu Date: Wed, 2 Dec 2026 12:42:55 +0900 Subject: [PATCH 1/2] Fix crash when wrapping a pandas NA scalar (#429) Supersedes #432. Co-authored-by: Vincent Gao --- tabulate/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 12a2950..573631b 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -1651,7 +1651,11 @@ def _wrap_text_to_colwidths( if cell is None else ( str(cell) - if cell == "" or _isnumber(cell) + # Guard the empty-string check with isinstance: + # comparing a scalar such as pandas' ``pd.NA`` to "" + # returns a non-bool whose truthiness raises + # "boolean value of NA is ambiguous". + if (isinstance(cell, str) and cell == "") or _isnumber(cell) else str(_type(cell, numparse)(cell)) ) ) From eb7a151afa057f874522cb93265957ea6add4e10 Mon Sep 17 00:00:00 2001 From: Lee Sang Gyu Date: Wed, 2 Dec 2026 12:43:22 +0900 Subject: [PATCH 2/2] Add test for wrapping a pandas NA scalar (#429) Supersedes #432. Co-authored-by: Vincent Gao --- test/test_textwrapper.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/test_textwrapper.py b/test/test_textwrapper.py index e6bab0f..f36804f 100644 --- a/test/test_textwrapper.py +++ b/test/test_textwrapper.py @@ -302,6 +302,40 @@ def test_wrap_none_value_with_missingval(): assert_equal(expected, result) +def test_wrap_pandas_na_value(): + """TextWrapper: a pandas NA scalar must wrap without crashing (issue #429).""" + try: + import pandas as pd + except ImportError: + skip("test_wrap_pandas_na_value is skipped") + + # ``pd.NA == ""`` returns NA, whose truthiness raises + # "boolean value of NA is ambiguous". Wrapping must render the cell the + # same way as the unwrapped path does (as ""). + data = [["First Entry", pd.NA], ["Second Entry", pd.NA]] + headers = ["Title", "Value"] + result = tabulate(data, headers=headers, tablefmt="grid", maxcolwidths=[7, 5]) + + expected = [ + "+---------+---------+", + "| Title | Value |", + "+=========+=========+", + "| First | |", + "| Entry | |", + "+---------+---------+", + "| Second | |", + "| Entry | |", + "+---------+---------+", + ] + expected = "\n".join(expected) + assert_equal(expected, result) + + # The cell text is the same with and without wrapping. + unwrapped = tabulate([["a", pd.NA]], tablefmt="plain") + wrapped = tabulate([["a", pd.NA]], tablefmt="plain", maxcolwidths=[7, 7]) + assert_equal(unwrapped, wrapped) + + def test_wrap_optional_bool_strs(): """TextWrapper: Show that str bools and None can be wrapped without crashing""" data = [