Skip to content

Commit e52c5ba

Browse files
committed
fix astanin#18 - display np.int64 numbers as integers, do not apply 'floatfmt'
1 parent 20c6370 commit e52c5ba

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

tabulate/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,8 +868,13 @@ def _isint(string, inttype=int):
868868
"""
869869
return (
870870
type(string) is inttype
871-
or isinstance(string, (bytes, str))
872-
and _isconvertible(inttype, string)
871+
or (
872+
hasattr(string, "is_integer")
873+
and str(type(string)).startswith("<class 'numpy.int")
874+
) # numpy.int64 and similar
875+
or (
876+
isinstance(string, (bytes, str)) and _isconvertible(inttype, string)
877+
) # integer as string
873878
)
874879

875880

test/test_regression.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,23 @@ def test_exception_on_empty_data_with_maxcolwidths():
492492
"Regression: exception on empty data when using maxcolwidths (github issue #180)"
493493
result = tabulate([], maxcolwidths=5)
494494
assert_equal(result, "")
495+
496+
497+
def test_numpy_int64_as_integer():
498+
"Regression: format numpy.int64 as integer (github issue #18)"
499+
try:
500+
import numpy as np
501+
502+
headers = ["int", "float"]
503+
table = [[np.int64(1), 3.14159]]
504+
result = tabulate(table, headers, tablefmt="pipe", floatfmt=".2f")
505+
expected = "\n".join(
506+
[
507+
"| int | float |",
508+
"|------:|--------:|",
509+
"| 1 | 3.14 |",
510+
]
511+
)
512+
assert_equal(result, expected)
513+
except ImportError:
514+
raise skip("")

0 commit comments

Comments
 (0)