Skip to content
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

BUG: Fix formatting of complex numbers with exponents #60417

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ Other
- Bug in :meth:`Series.dt` methods in :class:`ArrowDtype` that were returning incorrect values. (:issue:`57355`)
- Bug in :meth:`Series.rank` that doesn't preserve missing values for nullable integers when ``na_option='keep'``. (:issue:`56976`)
- Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` inconsistently replacing matching instances when ``regex=True`` and missing values are present. (:issue:`56599`)
- Bug in :meth:`Series.to_string` when series contains complex floats with exponents (:issue:`60405`)
- Bug in :meth:`read_csv` where chained fsspec TAR file and ``compression="infer"`` fails with ``tarfile.ReadError`` (:issue:`60028`)
- Bug in Dataframe Interchange Protocol implementation was returning incorrect results for data buffers' associated dtype, for string and datetime columns (:issue:`54781`)
- Bug in ``Series.list`` methods not preserving the original :class:`Index`. (:issue:`58425`)
Expand Down
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
18 changes: 18 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,24 @@ def test_to_string_complex_float_formatting(self):
)
assert result == expected

def test_to_string_complex_float_formatting_with_exponents(self):
# GH #60393
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