Skip to content

Commit 9dbe5dc

Browse files
committed
Fix formatting of complex floats with exponents
1 parent ee0902a commit 9dbe5dc

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

pandas/io/formats/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1749,7 +1749,7 @@ def _trim_zeros_complex(str_complexes: ArrayLike, decimal: str = ".") -> list[st
17491749
# The split will give [{"", "-"}, "xxx", "+/-", "xxx", "j", ""]
17501750
# Therefore, the imaginary part is the 4th and 3rd last elements,
17511751
# and the real part is everything before the imaginary part
1752-
trimmed = re.split(r"([j+-])", x)
1752+
trimmed = re.split(r"(?<!e)([j+-])", x)
17531753
real_part.append("".join(trimmed[:-4]))
17541754
imag_part.append("".join(trimmed[-4:-2]))
17551755

pandas/tests/io/formats/test_to_string.py

+17
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,23 @@ def test_to_string_complex_float_formatting(self):
422422
)
423423
assert result == expected
424424

425+
# GH #60393
426+
with option_context("display.precision", 6):
427+
df = DataFrame(
428+
{
429+
"x": [
430+
(1.8816e-09 + 0j),
431+
(1.8816e-09 + 3.39676e-09j),
432+
]
433+
}
434+
)
435+
result = df.to_string()
436+
expected = (
437+
" x\n0 1.881600e-09+0.000000e+00j\n"
438+
"1 1.881600e-09+3.396760e-09j"
439+
)
440+
assert result == expected
441+
425442
def test_to_string_format_inf(self):
426443
# GH#24861
427444
df = DataFrame(

0 commit comments

Comments
 (0)