Skip to content

Commit 7f27560

Browse files
mzeitlin11luckyvs1
authored andcommitted
BUG: float-like string, trailing 0 truncation (pandas-dev#38759)
* BUG: float-like string, trailing 0 truncation * Don't use _trim_zeros_float
1 parent 86b3c14 commit 7f27560

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

doc/source/whatsnew/v1.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- The deprecated attributes ``_AXIS_NAMES`` and ``_AXIS_NUMBERS`` of :class:`DataFrame` and :class:`Series` will no longer show up in ``dir`` or ``inspect.getmembers`` calls (:issue:`38740`)
1818
- :meth:`to_csv` created corrupted zip files when there were more rows than ``chunksize`` (issue:`38714`)
19+
- Bug in repr of float-like strings of an ``object`` dtype having trailing 0's truncated after the decimal (:issue:`38708`)
1920
-
2021

2122
.. ---------------------------------------------------------------------------

pandas/io/formats/format.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ def _format(x):
13001300
if not is_float_type[i] and leading_space:
13011301
fmt_values.append(f" {_format(v)}")
13021302
elif is_float_type[i]:
1303-
fmt_values.append(float_format(v))
1303+
fmt_values.append(_trim_zeros_single_float(float_format(v)))
13041304
else:
13051305
if leading_space is False:
13061306
# False specifically, so that the default is
@@ -1310,8 +1310,6 @@ def _format(x):
13101310
tpl = " {v}"
13111311
fmt_values.append(tpl.format(v=_format(v)))
13121312

1313-
fmt_values = _trim_zeros_float(str_floats=fmt_values, decimal=".")
1314-
13151313
return fmt_values
13161314

13171315

@@ -1827,11 +1825,25 @@ def _trim_zeros_complex(str_complexes: np.ndarray, decimal: str = ".") -> List[s
18271825
return padded
18281826

18291827

1828+
def _trim_zeros_single_float(str_float: str) -> str:
1829+
"""
1830+
Trims trailing zeros after a decimal point,
1831+
leaving just one if necessary.
1832+
"""
1833+
str_float = str_float.rstrip("0")
1834+
if str_float.endswith("."):
1835+
str_float += "0"
1836+
1837+
return str_float
1838+
1839+
18301840
def _trim_zeros_float(
18311841
str_floats: Union[np.ndarray, List[str]], decimal: str = "."
18321842
) -> List[str]:
18331843
"""
1834-
Trims zeros, leaving just one before the decimal points if need be.
1844+
Trims the maximum number of trailing zeros equally from
1845+
all numbers containing decimals, leaving just one if
1846+
necessary.
18351847
"""
18361848
trimmed = str_floats
18371849
number_regex = re.compile(fr"^\s*[\+-]?[0-9]+\{decimal}[0-9]*$")

pandas/tests/io/formats/test_format.py

+19
Original file line numberDiff line numberDiff line change
@@ -2005,6 +2005,25 @@ def test_float_trim_zeros(self):
20052005
assert ("+10" in line) or skip
20062006
skip = False
20072007

2008+
@pytest.mark.parametrize(
2009+
"data, expected",
2010+
[
2011+
(["3.50"], "0 3.50\ndtype: object"),
2012+
([1.20, "1.00"], "0 1.2\n1 1.00\ndtype: object"),
2013+
([np.nan], "0 NaN\ndtype: float64"),
2014+
([None], "0 None\ndtype: object"),
2015+
(["3.50", np.nan], "0 3.50\n1 NaN\ndtype: object"),
2016+
([3.50, np.nan], "0 3.5\n1 NaN\ndtype: float64"),
2017+
([3.50, np.nan, "3.50"], "0 3.5\n1 NaN\n2 3.50\ndtype: object"),
2018+
([3.50, None, "3.50"], "0 3.5\n1 None\n2 3.50\ndtype: object"),
2019+
],
2020+
)
2021+
def test_repr_str_float_truncation(self, data, expected):
2022+
# GH#38708
2023+
series = Series(data)
2024+
result = repr(series)
2025+
assert result == expected
2026+
20082027
def test_dict_entries(self):
20092028
df = DataFrame({"A": [{"a": 1, "b": 2}]})
20102029

0 commit comments

Comments
 (0)