Repository: pandas-dev/pandas

Issue:
Displayed precision of float in presence of np.inf
#### Code Sample, a copy-pastable example if possible

```python
import numpy as np
import pandas as pd

print(
    pd.Series([1., 2.2222, np.inf]).round(1)
)
```
#### Problem description

The display of floating precision in a dataframe and serie could be improved in the presence of `np.inf`.
My expectation would be that `np.inf` would induce the same behavior than `'NaN`.

#### Expected Output

```
0    1.0
1    2.2
2     inf
dtype: float64
```

Instead, the following result is obtained:

```
0    1.000000
1    2.200000
2         inf
dtype: float64
```

Note that this is the result in presence of a `NaN`:

```
0    1.0
1    2.2
2    NaN
dtype: float64
```

#### Output of ``pd.show_versions()``

<details>

```
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.7.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-43-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.4
pytest: None
pip: 10.0.1
setuptools: 39.2.0
Cython: None
numpy: 1.14.5
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: None
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
```

</details>



Discussion:

Resolving patch:
diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py
--- a/pandas/io/formats/format.py
+++ b/pandas/io/formats/format.py
@@ -1414,16 +1414,20 @@ def _trim_zeros(str_floats, na_rep='NaN'):
     """
     trimmed = str_floats
 
+    def _is_number(x):
+        return (x != na_rep and not x.endswith('inf'))
+
     def _cond(values):
-        non_na = [x for x in values if x != na_rep]
-        return (len(non_na) > 0 and all(x.endswith('0') for x in non_na) and
-                not (any(('e' in x) or ('E' in x) for x in non_na)))
+        finite = [x for x in values if _is_number(x)]
+        return (len(finite) > 0 and all(x.endswith('0') for x in finite) and
+                not (any(('e' in x) or ('E' in x) for x in finite)))
 
     while _cond(trimmed):
-        trimmed = [x[:-1] if x != na_rep else x for x in trimmed]
+        trimmed = [x[:-1] if _is_number(x) else x for x in trimmed]
 
     # leave one 0 after the decimal points if need be.
-    return [x + "0" if x.endswith('.') and x != na_rep else x for x in trimmed]
+    return [x + "0" if x.endswith('.') and _is_number(x) else x
+            for x in trimmed]
 
 
 def _has_names(index):


Test patch:
diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py
--- a/pandas/tests/io/formats/test_format.py
+++ b/pandas/tests/io/formats/test_format.py
@@ -1465,6 +1465,39 @@ def test_to_string_format_na(self):
                     '4  4.0     bar')
         assert result == expected
 
+    def test_to_string_format_inf(self):
+        # Issue #24861
+        tm.reset_display_options()
+        df = DataFrame({
+            'A': [-np.inf, np.inf, -1, -2.1234, 3, 4],
+            'B': [-np.inf, np.inf, 'foo', 'foooo', 'fooooo', 'bar']
+        })
+        result = df.to_string()
+
+        expected = ('        A       B\n'
+                    '0    -inf    -inf\n'
+                    '1     inf     inf\n'
+                    '2 -1.0000     foo\n'
+                    '3 -2.1234   foooo\n'
+                    '4  3.0000  fooooo\n'
+                    '5  4.0000     bar')
+        assert result == expected
+
+        df = DataFrame({
+            'A': [-np.inf, np.inf, -1., -2., 3., 4.],
+            'B': [-np.inf, np.inf, 'foo', 'foooo', 'fooooo', 'bar']
+        })
+        result = df.to_string()
+
+        expected = ('     A       B\n'
+                    '0 -inf    -inf\n'
+                    '1  inf     inf\n'
+                    '2 -1.0     foo\n'
+                    '3 -2.0   foooo\n'
+                    '4  3.0  fooooo\n'
+                    '5  4.0     bar')
+        assert result == expected
+
     def test_to_string_decimal(self):
         # Issue #23614
         df = DataFrame({'A': [6.0, 3.1, 2.2]})