Fix DataFrame.to_string() justification (2)
State: closed

- [x] closes #16839, 
closes #13032
- [ ] tests added / passed
- [ ] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry

'Competes' with #22437 which attempts to revert `% d` to `%d` as suggested here: https://github.com/pandas-dev/pandas/issues/13032#issue-151973347 That turned out to affect a lot of tests, which in hindsight is expected; the `% d` has been around since at least 2012 (106fe994cb0eb2701).

Instead, this PR reverts parts of #11942 and embraces the leading space even when `index=False`. `df.to_string(index=False)` will print the leading space when the first column is positive only, as well as preserve leading/trailing spaces on first/last lines.

With the following code:
```python
import pandas as pd
def wrap_to_string(df, **kwargs):
    s = df.to_string(**kwargs)
    print(str(kwargs).center(25, '-'))
    for i, line in enumerate(s.split('\n')):
        print(f'^{line}$-{i}')
    print()
df = pd.DataFrame({'w': [1, 2], 'x': [3, -4], 'y': [555, 666],
                   'z': [777, -888], 'a': ['AAA', '   ']})
cols_ = list(map(list, ['wxyza', 'xyzaw', 'yzawx', 'zawxy', 'awxyz']))
for cols in cols_:
    wrap_to_string(df[cols], index=False)
```
Output with master:

```python
-----{'index': False}----  # last cell (three spaces) disappeared
^w  x    y    z    a$-0
^1  3  555  777  AAA$-1
^2 -4  666 -888$-2

-----{'index': False}----  # misaligned
^x    y    z    a  w$-0
^3  555  777  AAA  1$-1
^-4  666 -888       2$-2

-----{'index': False}----  # misaligned
^y    z    a  w  x$-0
^555  777  AAA  1  3$-1
^666 -888       2 -4$-2

-----{'index': False}----  # misaligned
^z    a  w  x    y$-0
^777  AAA  1  3  555$-1
^-888       2 -4  666$-2

-----{'index': False}----  # misaligned
^a  w  x    y    z$-0
^AAA  1  3  555  777$-1
^     2 -4  666 -888$-2
```

Output with this PR:

```python
-----{'index': False}----
^ w  x    y    z    a$-0
^ 1  3  555  777  AAA$-1
^ 2 -4  666 -888     $-2

-----{'index': False}----
^ x    y    z    a  w$-0
^ 3  555  777  AAA  1$-1
^-4  666 -888       2$-2

-----{'index': False}----
^   y    z    a  w  x$-0
^ 555  777  AAA  1  3$-1
^ 666 -888       2 -4$-2

-----{'index': False}----
^   z    a  w  x    y$-0
^ 777  AAA  1  3  555$-1
^-888       2 -4  666$-2

-----{'index': False}----
^   a  w  x    y    z$-0
^ AAA  1  3  555  777$-1
^      2 -4  666 -888$-2
```

Similar effect on Series as well.


Labels:
name: Bug
name: IO Data
description: IO issues that don't fit into a more specific label
name: Output-Formatting
description: __repr__ of pandas objects, to_string

Comments:
cc @datapythonista 
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22505?src=pr&el=h1) Report
> Merging [#22505](https://codecov.io/gh/pandas-dev/pandas/pull/22505?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/64b88e86677349de66071dffd74b5c3254283e2a?src=pr&el=desc) will **decrease** coverage by `0.15%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/22505/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/22505?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #22505      +/-   ##
==========================================
- Coverage   92.18%   92.03%   -0.16%     
==========================================
  Files         169      169              
  Lines       50820    50778      -42     
==========================================
- Hits        46850    46735     -115     
- Misses       3970     4043      +73
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.44% <100%> (-0.16%)` | :arrow_down: |
| #single | `42.22% <0%> (-0.16%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/22505?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/formats/format.py](https://codecov.io/gh/pandas-dev/pandas/pull/22505/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvZm9ybWF0LnB5) | `98.35% <100%> (-0.01%)` | :arrow_down: |
| [pandas/io/formats/console.py](https://codecov.io/gh/pandas-dev/pandas/pull/22505/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvY29uc29sZS5weQ==) | `65.15% <0%> (-10.61%)` | :arrow_down: |
| [pandas/errors/\_\_init\_\_.py](https://codecov.io/gh/pandas-dev/pandas/pull/22505/diff?src=pr&el=tree#diff-cGFuZGFzL2Vycm9ycy9fX2luaXRfXy5weQ==) | `92.3% <0%> (-7.7%)` | :arrow_down: |
| [pandas/core/dtypes/base.py](https://codecov.io/gh/pandas-dev/pandas/pull/22505/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZHR5cGVzL2Jhc2UucHk=) | `92.68% <0%> (-7.32%)` | :arrow_down: |
| [pandas/core/arrays/base.py](https://codecov.io/gh/pandas-dev/pandas/pull/22505/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL2Jhc2UucHk=) | `88% <0%> (-6.25%)` | :arrow_down: |
| [pandas/io/html.py](https://codecov.io/gh/pandas-dev/pandas/pull/22505/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2h0bWwucHk=) | `89.17% <0%> (-2.08%)` | :arrow_down: |
| [pandas/io/parquet.py](https://codecov.io/gh/pandas-dev/pandas/pull/22505/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL3BhcnF1ZXQucHk=) | `71.79% <0%> (-1.94%)` | :arrow_down: |
| [pandas/io/formats/html.py](https://codecov.io/gh/pandas-dev/pandas/pull/22505/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvaHRtbC5weQ==) | `88.81% <0%> (-1.87%)` | :arrow_down: |
| [pandas/core/apply.py](https://codecov.io/gh/pandas-dev/pandas/pull/22505/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXBwbHkucHk=) | `96.75% <0%> (-1.86%)` | :arrow_down: |
| [pandas/core/arrays/datetimelike.py](https://codecov.io/gh/pandas-dev/pandas/pull/22505/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL2RhdGV0aW1lbGlrZS5weQ==) | `94.02% <0%> (-1.52%)` | :arrow_down: |
| ... and [51 more](https://codecov.io/gh/pandas-dev/pandas/pull/22505/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22505?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22505?src=pr&el=footer). Last update [64b88e8...cc86cd7](https://codecov.io/gh/pandas-dev/pandas/pull/22505?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

lgtm, but I think the changelog needs to be moved to `0.24.0`.

@jreback can you take a look and see if you're happy with this?
Hello @gshiba! Thanks for updating the PR.

 - There are no PEP8 issues in the file [`pandas/io/formats/format.py`](https://github.com/gshiba/pandas/blob/d4ac415c465334198d6d26c85b05f24f4b4a233f/pandas/io/formats/format.py) !

 - There are no PEP8 issues in the file [`pandas/tests/io/formats/test_format.py`](https://github.com/gshiba/pandas/blob/d4ac415c465334198d6d26c85b05f24f4b4a233f/pandas/tests/io/formats/test_format.py) !


thanks @gshiba