From f57ecab472ff319defcbd379da61ea73477a185d Mon Sep 17 00:00:00 2001 From: Sskift <163287349+Sskift@users.noreply.github.com> Date: Tue, 3 Nov 2026 02:00:03 +0800 Subject: [PATCH 1/2] Preserve NumPy unsigned integer formatting and precision --- README.md | 3 ++- tabulate/__init__.py | 8 +++++--- test/test_numpy_integers.py | 39 +++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 test/test_numpy_integers.py diff --git a/README.md b/README.md index 0283a0c..ec18b22 100644 --- a/README.md +++ b/README.md @@ -766,7 +766,8 @@ column, in which case every column may have different number formatting: ``` -`intfmt` works similarly for integers +`intfmt` works similarly for integers, including signed and unsigned NumPy +integer scalars: >>> print(tabulate([["a",1000],["b",90000]], intfmt=",")) - ------ diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 12a2950..3f0dd9f 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -11,6 +11,7 @@ import io from itertools import chain, zip_longest as izip_longest import math +from numbers import Integral import re import textwrap import warnings @@ -935,9 +936,10 @@ def _isint(string, inttype=int): return ( type(string) is inttype or ( - (hasattr(string, "is_integer") or hasattr(string, "__array__")) - and str(type(string)).startswith(" Date: Tue, 3 Nov 2026 02:02:52 +0800 Subject: [PATCH 2/2] Keep NumPy durations out of integer formatting --- test/test_numpy_integers.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_numpy_integers.py b/test/test_numpy_integers.py index daaa4d0..24aab15 100644 --- a/test/test_numpy_integers.py +++ b/test/test_numpy_integers.py @@ -37,3 +37,10 @@ def test_numpy_integer_formats(input_type, intfmt, dtype): options = {"tablefmt": "plain", "showindex": False, "intfmt": intfmt, "floatfmt": ".0f"} assert tabulate(table, **options) == tabulate([[int(value)]], **options) + + +def test_numpy_duration_is_not_formatted_as_integer(): + numpy = pytest.importorskip("numpy") + value = numpy.timedelta64(1, "D") + + assert tabulate([[value]], tablefmt="plain", intfmt="x") == str(value)