Skip to content

Commit a9a22e9

Browse files
committed
Merge python-tabulate into feature/named-dataframe-index
2 parents 99e4d5d + 6df0614 commit a9a22e9

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

tabulate.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,12 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"):
711711
rows = list(izip_longest(*tabular_data.values())) # columns have to be transposed
712712
elif hasattr(tabular_data, "index"):
713713
# values is a property, has .index => it's likely a pandas.DataFrame (pandas 0.11.0)
714-
keys = tabular_data.keys()
714+
keys = list(tabular_data)
715+
if tabular_data.index.name is not None:
716+
if isinstance(tabular_data.index.name, list):
717+
keys[:0] = tabular_data.index.name
718+
else:
719+
keys[:0] = [tabular_data.index.name]
715720
vals = tabular_data.values # values matrix doesn't need to be transposed
716721
# for DataFrames add an index per default
717722
index = list(tabular_data.index)
@@ -1068,6 +1073,9 @@ def tabulate(tabular_data, headers=(), tablefmt="simple",
10681073
list_of_lists, headers = _normalize_tabular_data(
10691074
tabular_data, headers, showindex=showindex)
10701075

1076+
if tablefmt == 'rst' and len(headers) > 0 and headers[0] == '':
1077+
headers[0] = '-'
1078+
10711079
# optimization: look for ANSI control codes once,
10721080
# enable smart width functions only if a control code is found
10731081
plain_text = '\n'.join(['\t'.join(map(_text_type, headers))] + \

test/test_output.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,49 @@ def test_pandas_without_index():
455455
raise SkipTest() # this test is optional
456456

457457

458+
def test_pandas_rst_with_index():
459+
"Output: a pandas Dataframe with an index in ReStructuredText format"
460+
try:
461+
import pandas
462+
df = pandas.DataFrame([["one", 1], ["two", None]],
463+
columns=["string", "number"],
464+
index=["a", "b"])
465+
expected = "\n".join(
466+
['=== ======== ========',
467+
'- string number',
468+
'=== ======== ========',
469+
'a one 1',
470+
'b two nan',
471+
'=== ======== ========'])
472+
result = tabulate(df, tablefmt="rst", headers="keys")
473+
assert_equal(expected, result)
474+
except ImportError:
475+
print("test_pandas_rst_with_index is skipped")
476+
raise SkipTest() # this test is optional
477+
478+
479+
def test_pandas_rst_with_named_index():
480+
"Output: a pandas Dataframe with a named index in ReStructuredText format"
481+
try:
482+
import pandas
483+
index = pandas.Index(["a", "b"], name='index')
484+
df = pandas.DataFrame([["one", 1], ["two", None]],
485+
columns=["string", "number"],
486+
index=index)
487+
expected = "\n".join(
488+
['======= ======== ========',
489+
'index string number',
490+
'======= ======== ========',
491+
'a one 1',
492+
'b two nan',
493+
'======= ======== ========'])
494+
result = tabulate(df, tablefmt="rst", headers="keys")
495+
assert_equal(expected, result)
496+
except ImportError:
497+
print("test_pandas_rst_with_index is skipped")
498+
raise SkipTest() # this test is optional
499+
500+
458501
def test_dict_like_with_index():
459502
"Output: a table with a running index"
460503
dd = {"b": range(101,104)}

0 commit comments

Comments
 (0)