Skip to content

BUG: Fix formatting of complex floats with exponents #60405

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

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 1 addition & 1 deletion pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,7 @@ def _trim_zeros_complex(str_complexes: ArrayLike, decimal: str = ".") -> list[st
# The split will give [{"", "-"}, "xxx", "+/-", "xxx", "j", ""]
# Therefore, the imaginary part is the 4th and 3rd last elements,
# and the real part is everything before the imaginary part
trimmed = re.split(r"([j+-])", x)
trimmed = re.split(r"(?<!e)([j+-])", x)
real_part.append("".join(trimmed[:-4]))
imag_part.append("".join(trimmed[-4:-2]))

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/io/formats/test_to_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,23 @@ def test_to_string_complex_float_formatting(self):
)
assert result == expected

# GH #60393
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this a separate test?

with option_context("display.precision", 6):
df = DataFrame(
{
"x": [
(1.8816e-09 + 0j),
(1.8816e-09 + 3.39676e-09j),
]
}
)
result = df.to_string()
expected = (
" x\n0 1.881600e-09+0.000000e+00j\n"
"1 1.881600e-09+3.396760e-09j"
)
assert result == expected

def test_to_string_format_inf(self):
# GH#24861
df = DataFrame(
Expand Down