BUG: Fix float formatting when a string is passed as float_format arg
State: closed

closes #21625
closes #22270.

If a string `float_format` argument is passed by the user to `to_string`, `to_latex`, `to_html`, etc, then disable fixed width, so that now e.g.
```python
df.to_string(float_format='%.3f')
```
will give the same result as 
```python
df.to_string(float_format=lambda x: '%.3f' % x)
```

- [x] closes #21625 and #22270 
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Labels:
name: Output-Formatting
description: __repr__ of pandas objects, to_string

Comments:
can you add the test from #21625 as well
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22308?src=pr&el=h1) Report
> Merging [#22308](https://codecov.io/gh/pandas-dev/pandas/pull/22308?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/e5c90e5e355077c4ee5245a4a72833f4e8af72b9?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master   #22308      +/-   ##
==========================================
+ Coverage   92.29%   92.29%   +<.01%     
==========================================
  Files         161      161              
  Lines       51497    51501       +4     
==========================================
+ Hits        47530    47534       +4     
  Misses       3967     3967
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.69% <100%> (ø)` | :arrow_up: |
| #single | `42.42% <0%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/22308?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/formats/format.py](https://codecov.io/gh/pandas-dev/pandas/pull/22308/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvZm9ybWF0LnB5) | `97.76% <100%> (ø)` | :arrow_up: |
| [pandas/io/pytables.py](https://codecov.io/gh/pandas-dev/pandas/pull/22308/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL3B5dGFibGVzLnB5) | `92.3% <0%> (-0.05%)` | :arrow_down: |
| [pandas/core/reshape/pivot.py](https://codecov.io/gh/pandas-dev/pandas/pull/22308/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvcmVzaGFwZS9waXZvdC5weQ==) | `96.55% <0%> (ø)` | :arrow_up: |
| [pandas/io/json/normalize.py](https://codecov.io/gh/pandas-dev/pandas/pull/22308/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2pzb24vbm9ybWFsaXplLnB5) | `96.87% <0%> (ø)` | :arrow_up: |
| [pandas/io/packers.py](https://codecov.io/gh/pandas-dev/pandas/pull/22308/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL3BhY2tlcnMucHk=) | `88.08% <0%> (ø)` | :arrow_up: |
| [pandas/core/util/hashing.py](https://codecov.io/gh/pandas-dev/pandas/pull/22308/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvdXRpbC9oYXNoaW5nLnB5) | `98.4% <0%> (ø)` | :arrow_up: |
| [pandas/core/algorithms.py](https://codecov.io/gh/pandas-dev/pandas/pull/22308/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYWxnb3JpdGhtcy5weQ==) | `95.11% <0%> (ø)` | :arrow_up: |
| [pandas/util/\_decorators.py](https://codecov.io/gh/pandas-dev/pandas/pull/22308/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvX2RlY29yYXRvcnMucHk=) | `91.34% <0%> (ø)` | :arrow_up: |
| [pandas/tseries/offsets.py](https://codecov.io/gh/pandas-dev/pandas/pull/22308/diff?src=pr&el=tree#diff-cGFuZGFzL3RzZXJpZXMvb2Zmc2V0cy5weQ==) | `96.98% <0%> (ø)` | :arrow_up: |
| [pandas/core/indexes/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/22308/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZXMucHk=) | `96.2% <0%> (ø)` | :arrow_up: |
| ... and [20 more](https://codecov.io/gh/pandas-dev/pandas/pull/22308/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22308?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/22308?src=pr&el=footer). Last update [e5c90e5...a9666bd](https://codecov.io/gh/pandas-dev/pandas/pull/22308?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

can you rebase
can you rebase and see if you can address comments above
Sorry I am away at the moment. I can have a look at this next week.
can you merge master and update to comments
Hello @tomneep! Thanks for updating the PR.

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

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

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

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



##### Comment last updated on November 26, 2018 at 12:27 Hours UTC
Changes above are just merging with the current master. 
Sorry it's taken me so long to get back to this, I have some time now. 

With fresh eyes I agree this probably isn't the best way to solve this but I'm a bit stuck how better to proceed. 

grepping the code I see that `fixed_width` isn't used outside of the `FloatArrayFormatter` (other than appearing in the base class `__init__`), but there are a couple of instances of `FloatArrayFormatter` created in `pandas/core/indexes/numeric.py` and `pandas/core/internals/blocks.py` with `fixed_width=False`. 

A slightly different way of doing essentially the same thing (but without touching the `*ArrayFormatter`s) would be to change `format_array` (https://github.com/pandas-dev/pandas/blob/master/pandas/io/formats/format.py#L846). For example, adding `fixed_width=float_format is None` to the `fmt_klass` instance has the same effect, with all tests passing. This is maybe slightly nicer, as you could still directly create a `FloatArrayFormatter` with a value of `float_format` and `fixed_width=True`.

Any suggestions would be welcome.
the way the formatters is done now is not super flexible, so your change is ok. if you can add tests for latex & html would be good.
thanks!