Skip to content

Commit ee2de9f

Browse files
committed
Fix a bug that header shifts wrongly when do tabulate(headers='keys', showindex=False) for pandas DataFrame with named index
1 parent 2552e6d commit ee2de9f

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

tabulate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"):
10221022
elif hasattr(tabular_data, "index"):
10231023
# values is a property, has .index => it's likely a pandas.DataFrame (pandas 0.11.0)
10241024
keys = list(tabular_data)
1025-
if tabular_data.index.name is not None:
1025+
if showindex in ["default", "always", True] and tabular_data.index.name is not None:
10261026
if isinstance(tabular_data.index.name, list):
10271027
keys[:0] = tabular_data.index.name
10281028
else:

test/test_output.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,8 @@ def test_pandas_without_index():
13631363
import pandas
13641364

13651365
df = pandas.DataFrame(
1366-
[["one", 1], ["two", None]], columns=["string", "number"], index=["a", "b"]
1366+
[["one", 1], ["two", None]], columns=["string", "number"],
1367+
index=pandas.Index(["a", "b"], name="index")
13671368
)
13681369
expected = "\n".join(
13691370
[

0 commit comments

Comments
 (0)