Skip to content

Commit 8e1cc56

Browse files
authored
BUG: fix inconsistent col spacing in info (#36766)
1 parent da3a2d3 commit 8e1cc56

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

pandas/io/formats/info.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,11 @@ def _verbose_repr(
288288
len_column = len(pprint_thing(column_head))
289289
space = max(max_col, len_column) + col_space
290290

291-
max_id = len(pprint_thing(col_count))
291+
# GH #36765
292+
# add one space in max_id because there is a one-space padding
293+
# in front of the number
294+
# this allows maintain two spaces gap between columns
295+
max_id = len(pprint_thing(col_count)) + 1
292296
len_id = len(pprint_thing(id_head))
293297
space_num = max(max_id, len_id) + col_space
294298

pandas/tests/io/formats/test_info.py

+59-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def test_info_verbose():
8787
frame.info(verbose=True, buf=buf)
8888

8989
res = buf.getvalue()
90-
header = " # Column Dtype \n--- ------ ----- "
90+
header = " # Column Dtype \n--- ------ ----- "
9191
assert header in res
9292

9393
frame.info(verbose=True, buf=buf)
@@ -101,6 +101,64 @@ def test_info_verbose():
101101
assert line.startswith(line_nr)
102102

103103

104+
@pytest.mark.parametrize(
105+
"size, header_exp, separator_exp, first_line_exp, last_line_exp",
106+
[
107+
(
108+
4,
109+
" # Column Non-Null Count Dtype ",
110+
"--- ------ -------------- ----- ",
111+
" 0 0 3 non-null float64",
112+
" 3 3 3 non-null float64",
113+
),
114+
(
115+
11,
116+
" # Column Non-Null Count Dtype ",
117+
"--- ------ -------------- ----- ",
118+
" 0 0 3 non-null float64",
119+
" 10 10 3 non-null float64",
120+
),
121+
(
122+
101,
123+
" # Column Non-Null Count Dtype ",
124+
"--- ------ -------------- ----- ",
125+
" 0 0 3 non-null float64",
126+
" 100 100 3 non-null float64",
127+
),
128+
(
129+
1001,
130+
" # Column Non-Null Count Dtype ",
131+
"--- ------ -------------- ----- ",
132+
" 0 0 3 non-null float64",
133+
" 1000 1000 3 non-null float64",
134+
),
135+
(
136+
10001,
137+
" # Column Non-Null Count Dtype ",
138+
"--- ------ -------------- ----- ",
139+
" 0 0 3 non-null float64",
140+
" 10000 10000 3 non-null float64",
141+
),
142+
],
143+
)
144+
def test_info_verbose_with_counts_spacing(
145+
size, header_exp, separator_exp, first_line_exp, last_line_exp
146+
):
147+
"""Test header column, spacer, first line and last line in verbose mode."""
148+
frame = DataFrame(np.random.randn(3, size))
149+
buf = StringIO()
150+
frame.info(verbose=True, null_counts=True, buf=buf)
151+
all_lines = buf.getvalue().splitlines()
152+
# Here table would contain only header, separator and table lines
153+
# dframe repr, index summary, memory usage and dtypes are excluded
154+
table = all_lines[3:-2]
155+
header, separator, first_line, *rest, last_line = table
156+
assert header == header_exp
157+
assert separator == separator_exp
158+
assert first_line == first_line_exp
159+
assert last_line == last_line_exp
160+
161+
104162
def test_info_memory():
105163
# https://github.com/pandas-dev/pandas/issues/21056
106164
df = DataFrame({"a": Series([1, 2], dtype="i8")})

0 commit comments

Comments
 (0)