Skip to content

BUG: fix inconsistent col spacing in info #36766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pandas/io/formats/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ def _verbose_repr(
len_column = len(pprint_thing(column_head))
space = max(max_col, len_column) + col_space

max_id = len(pprint_thing(col_count))
# GH #36765
# add one space in max_id because there is a one-space padding
# in front of the number
# this allows maintain two spaces gap between columns
max_id = len(pprint_thing(col_count)) + 1
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this was starting playing role only when number of columns was over 1000. Because below 1000 max_id < len_id. Therefore below 1000 columns there were no issues.

len_id = len(pprint_thing(id_head))
space_num = max(max_id, len_id) + col_space

Expand Down
60 changes: 59 additions & 1 deletion pandas/tests/io/formats/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_info_verbose():
frame.info(verbose=True, buf=buf)

res = buf.getvalue()
header = " # Column Dtype \n--- ------ ----- "
header = " # Column Dtype \n--- ------ ----- "
assert header in res

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


@pytest.mark.parametrize(
"size, header_exp, separator_exp, first_line_exp, last_line_exp",
[
(
4,
" # Column Non-Null Count Dtype ",
"--- ------ -------------- ----- ",
" 0 0 3 non-null float64",
" 3 3 3 non-null float64",
),
(
11,
" # Column Non-Null Count Dtype ",
"--- ------ -------------- ----- ",
" 0 0 3 non-null float64",
" 10 10 3 non-null float64",
),
(
101,
" # Column Non-Null Count Dtype ",
"--- ------ -------------- ----- ",
" 0 0 3 non-null float64",
" 100 100 3 non-null float64",
),
(
1001,
" # Column Non-Null Count Dtype ",
"--- ------ -------------- ----- ",
" 0 0 3 non-null float64",
" 1000 1000 3 non-null float64",
),
(
10001,
" # Column Non-Null Count Dtype ",
"--- ------ -------------- ----- ",
" 0 0 3 non-null float64",
" 10000 10000 3 non-null float64",
),
],
)
def test_info_verbose_with_counts_spacing(
size, header_exp, separator_exp, first_line_exp, last_line_exp
):
"""Test header column, spacer, first line and last line in verbose mode."""
frame = DataFrame(np.random.randn(3, size))
buf = StringIO()
frame.info(verbose=True, null_counts=True, buf=buf)
all_lines = buf.getvalue().splitlines()
# Here table would contain only header, separator and table lines
# dframe repr, index summary, memory usage and dtypes are excluded
table = all_lines[3:-2]
header, separator, first_line, *rest, last_line = table
assert header == header_exp
assert separator == separator_exp
assert first_line == first_line_exp
assert last_line == last_line_exp


def test_info_memory():
# https://github.com/pandas-dev/pandas/issues/21056
df = DataFrame({"a": Series([1, 2], dtype="i8")})
Expand Down