diff --git a/pandas/io/formats/info.py b/pandas/io/formats/info.py index e8e41d4325103..9812ca39e2656 100644 --- a/pandas/io/formats/info.py +++ b/pandas/io/formats/info.py @@ -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 len_id = len(pprint_thing(id_head)) space_num = max(max_id, len_id) + col_space diff --git a/pandas/tests/io/formats/test_info.py b/pandas/tests/io/formats/test_info.py index 7000daeb9b575..d98530b5435e7 100644 --- a/pandas/tests/io/formats/test_info.py +++ b/pandas/tests/io/formats/test_info.py @@ -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) @@ -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")})