Skip to content

BUG: Fix FloatingArray output formatting #36800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ca6c979
BUG: Fix FloatingArray output formatting
dsaxton Oct 1, 2020
adcc26c
fixup
dsaxton Oct 1, 2020
4663e10
fixup
dsaxton Oct 1, 2020
406ed4d
CLN: Clean float / complex string formatting
dsaxton Oct 2, 2020
a1228c9
Fix
dsaxton Oct 2, 2020
04f4be8
Merge branch 'fix-is-numeric-helper' into nullable-float-array-string…
dsaxton Oct 2, 2020
77b88fc
Test
dsaxton Oct 2, 2020
8743b5b
Test
dsaxton Oct 2, 2020
a235611
Fixture
dsaxton Oct 2, 2020
d775c33
Update format.py
dsaxton Oct 11, 2020
00c15ba
Reapply
dsaxton Oct 11, 2020
06ef337
Update
dsaxton Oct 11, 2020
aedd7ab
Merge remote-tracking branch 'upstream/master' into nullable-float-ar…
dsaxton Oct 11, 2020
32264e9
Hack
dsaxton Oct 12, 2020
ba3ac46
Simplify
dsaxton Oct 12, 2020
f8a3216
Fix regex
dsaxton Oct 12, 2020
546b44e
Fix doc
dsaxton Oct 12, 2020
99d6905
Fix
dsaxton Oct 12, 2020
319529f
Again
dsaxton Oct 12, 2020
f65bcfe
Merge remote-tracking branch 'upstream/master' into nullable-float-ar…
dsaxton Oct 14, 2020
7a477dc
Fix merge
dsaxton Oct 14, 2020
13567f8
Merge remote-tracking branch 'upstream/master' into nullable-float-ar…
dsaxton Oct 14, 2020
253c94e
Merge remote-tracking branch 'upstream/master' into nullable-float-ar…
dsaxton Oct 14, 2020
2136494
Merge remote-tracking branch 'upstream/master' into nullable-float-ar…
dsaxton Oct 15, 2020
7d4452a
Type and doc
dsaxton Oct 15, 2020
80b0103
Oops
dsaxton Oct 15, 2020
faa053f
Merge remote-tracking branch 'upstream/master' into nullable-float-ar…
dsaxton Oct 15, 2020
8fd5a9f
Add tests
dsaxton Oct 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ def _format_strings(self) -> List[str]:
float_format = get_option("display.float_format")
if float_format is None:
precision = get_option("display.precision")
float_format = lambda x: f"{x: .{precision:d}g}"
float_format = lambda x: f"{x: .{precision:d}f}"
else:
float_format = self.float_format

Expand Down Expand Up @@ -1372,6 +1372,8 @@ def _format(x):
tpl = " {v}"
fmt_values.append(tpl.format(v=_format(v)))

fmt_values = _trim_zeros_float(str_floats=fmt_values, decimal=".")

return fmt_values


Expand Down Expand Up @@ -1473,9 +1475,9 @@ def format_values_with(float_format):

if self.fixed_width:
if is_complex:
result = _trim_zeros_complex(values, self.decimal, na_rep)
result = values
else:
result = _trim_zeros_float(values, self.decimal, na_rep)
result = _trim_zeros_float(values, self.decimal)
return np.asarray(result, dtype="object")

return values
Expand Down Expand Up @@ -1855,29 +1857,16 @@ def just(x):
return result


def _trim_zeros_complex(
str_complexes: np.ndarray, decimal: str = ".", na_rep: str = "NaN"
) -> List[str]:
"""
Separates the real and imaginary parts from the complex number, and
executes the _trim_zeros_float method on each of those.
"""
return [
"".join(_trim_zeros_float(re.split(r"([j+-])", x), decimal, na_rep))
for x in str_complexes
]


def _trim_zeros_float(
str_floats: Union[np.ndarray, List[str]], decimal: str = ".", na_rep: str = "NaN"
str_floats: Union[np.ndarray, List[str]], decimal: str = "."
) -> List[str]:
"""
Trims zeros, leaving just one before the decimal points if need be.
"""
trimmed = str_floats

def _is_number(x):
return x != na_rep and not x.endswith("inf")
return re.match(r"\s*-?[0-9]+(\.[0-9]*)?", x) is not None

def _cond(values):
finite = [x for x in values if _is_number(x)]
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3432,3 +3432,14 @@ def test_format_remove_leading_space_dataframe(input_array, expected):
# GH: 24980
df = pd.DataFrame(input_array).to_string(index=False)
assert df == expected


@pytest.mark.parametrize("dtype", ["Float32", "Float64"])
def test_nullable_float_to_string(dtype):
# https://github.com/pandas-dev/pandas/issues/36775
s = pd.Series([0.0, 1.0, None], dtype=dtype)
result = s.to_string()
expected = """0 0.0
1 1.0
2 <NA>"""
assert result == expected