Skip to content

Commit a58e478

Browse files
committed
fix astanin#62: allow numpy arrays and pandas Index as headers
1 parent 503368e commit a58e478

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

tabulate.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,13 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"):
649649
650650
"""
651651

652+
try:
653+
bool(headers)
654+
is_headers2bool_broken = False
655+
except ValueError: # numpy.ndarray, pandas.core.index.Index, ...
656+
is_headers2bool_broken = True
657+
headers = list(headers)
658+
652659
index = None
653660
if hasattr(tabular_data, "keys") and hasattr(tabular_data, "values"):
654661
# dict-like and pandas.DataFrame?

test/test_regression.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,6 @@ def test_long_integers():
216216
assert_equal(result, expected)
217217

218218

219-
def test_align_long_integers():
220-
"Regression: long integers should be aligned as integers (issue #61)"
221-
table = [[_long_type(1)], [_long_type(234)]]
222-
result = tabulate(table, tablefmt="plain")
223-
expected = "\n".join([" 1",
224-
"234"])
225-
assert_equal(result, expected)
226-
227-
228219
def test_colorclass_colors():
229220
"Regression: ANSI colors in a unicode/str subclass (issue #49)"
230221
try:
@@ -240,3 +231,24 @@ class textclass(_text_type):
240231
result = tabulate([[s]], tablefmt="plain")
241232
expected = "\x1b[35m3.14\x1b[39m"
242233
assert_equal(result, expected)
234+
235+
236+
def test_align_long_integers():
237+
"Regression: long integers should be aligned as integers (issue #61)"
238+
table = [[_long_type(1)], [_long_type(234)]]
239+
result = tabulate(table, tablefmt="plain")
240+
expected = "\n".join([" 1",
241+
"234"])
242+
assert_equal(result, expected)
243+
244+
245+
def test_numpy_array_as_headers():
246+
"Regression: NumPy array used as headers (issue #62)"
247+
try:
248+
import numpy as np
249+
headers = np.array(["foo", "bar"])
250+
result = tabulate([], headers, tablefmt="plain")
251+
expected = "foo bar"
252+
assert_equal(result, expected)
253+
except ImportError:
254+
raise SkipTest()

0 commit comments

Comments
 (0)