Skip to content

Commit f0a7d18

Browse files
authored
Fix strange behavior when precision display option is zero (#20359) (#35212)
1 parent c7626b1 commit f0a7d18

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,7 @@ MultiIndex
10181018

10191019
I/O
10201020
^^^
1021+
- Bug in print-out when ``display.precision`` is zero. (:issue:`20359`)
10211022
- Bug in :meth:`read_json` where integer overflow was occurring when json contains big number strings. (:issue:`30320`)
10221023
- `read_csv` will now raise a ``ValueError`` when the arguments `header` and `prefix` both are not `None`. (:issue:`27394`)
10231024
- Bug in :meth:`DataFrame.to_json` was raising ``NotFoundError`` when ``path_or_buf`` was an S3 URI (:issue:`28375`)

pandas/io/formats/format.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -1382,9 +1382,9 @@ def format_values_with(float_format):
13821382

13831383
if self.fixed_width:
13841384
if is_complex:
1385-
result = _trim_zeros_complex(values, na_rep)
1385+
result = _trim_zeros_complex(values, self.decimal, na_rep)
13861386
else:
1387-
result = _trim_zeros_float(values, na_rep)
1387+
result = _trim_zeros_float(values, self.decimal, na_rep)
13881388
return np.asarray(result, dtype="object")
13891389

13901390
return values
@@ -1754,19 +1754,21 @@ def just(x):
17541754
return result
17551755

17561756

1757-
def _trim_zeros_complex(str_complexes: np.ndarray, na_rep: str = "NaN") -> List[str]:
1757+
def _trim_zeros_complex(
1758+
str_complexes: np.ndarray, decimal: str = ".", na_rep: str = "NaN"
1759+
) -> List[str]:
17581760
"""
17591761
Separates the real and imaginary parts from the complex number, and
17601762
executes the _trim_zeros_float method on each of those.
17611763
"""
17621764
return [
1763-
"".join(_trim_zeros_float(re.split(r"([j+-])", x), na_rep))
1765+
"".join(_trim_zeros_float(re.split(r"([j+-])", x), decimal, na_rep))
17641766
for x in str_complexes
17651767
]
17661768

17671769

17681770
def _trim_zeros_float(
1769-
str_floats: Union[np.ndarray, List[str]], na_rep: str = "NaN"
1771+
str_floats: Union[np.ndarray, List[str]], decimal: str = ".", na_rep: str = "NaN"
17701772
) -> List[str]:
17711773
"""
17721774
Trims zeros, leaving just one before the decimal points if need be.
@@ -1778,8 +1780,11 @@ def _is_number(x):
17781780

17791781
def _cond(values):
17801782
finite = [x for x in values if _is_number(x)]
1783+
has_decimal = [decimal in x for x in finite]
1784+
17811785
return (
17821786
len(finite) > 0
1787+
and all(has_decimal)
17831788
and all(x.endswith("0") for x in finite)
17841789
and not (any(("e" in x) or ("E" in x) for x in finite))
17851790
)
@@ -1788,7 +1793,7 @@ def _cond(values):
17881793
trimmed = [x[:-1] if _is_number(x) else x for x in trimmed]
17891794

17901795
# leave one 0 after the decimal points if need be.
1791-
return [x + "0" if x.endswith(".") and _is_number(x) else x for x in trimmed]
1796+
return [x + "0" if x.endswith(decimal) and _is_number(x) else x for x in trimmed]
17921797

17931798

17941799
def _has_names(index: Index) -> bool:

pandas/tests/io/formats/test_format.py

+9
Original file line numberDiff line numberDiff line change
@@ -2910,6 +2910,15 @@ def test_format(self):
29102910
assert result[0] == " 12.0"
29112911
assert result[1] == " 0.0"
29122912

2913+
def test_output_display_precision_trailing_zeroes(self):
2914+
# Issue #20359: trimming zeros while there is no decimal point
2915+
2916+
# Happens when display precision is set to zero
2917+
with pd.option_context("display.precision", 0):
2918+
s = pd.Series([840.0, 4200.0])
2919+
expected_output = "0 840\n1 4200\ndtype: float64"
2920+
assert str(s) == expected_output
2921+
29132922
def test_output_significant_digits(self):
29142923
# Issue #9764
29152924

0 commit comments

Comments
 (0)