Skip to content

Commit ae77798

Browse files
snitishKevsterAmp
authored andcommitted
BUG: Fix formatting of complex numbers with exponents (pandas-dev#60417)
Fix formatting of complex numbers with exponents
1 parent b736e83 commit ae77798

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ Other
789789
- Bug in :meth:`Series.dt` methods in :class:`ArrowDtype` that were returning incorrect values. (:issue:`57355`)
790790
- Bug in :meth:`Series.rank` that doesn't preserve missing values for nullable integers when ``na_option='keep'``. (:issue:`56976`)
791791
- Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` inconsistently replacing matching instances when ``regex=True`` and missing values are present. (:issue:`56599`)
792+
- Bug in :meth:`Series.to_string` when series contains complex floats with exponents (:issue:`60405`)
792793
- Bug in :meth:`read_csv` where chained fsspec TAR file and ``compression="infer"`` fails with ``tarfile.ReadError`` (:issue:`60028`)
793794
- Bug in Dataframe Interchange Protocol implementation was returning incorrect results for data buffers' associated dtype, for string and datetime columns (:issue:`54781`)
794795
- Bug in ``Series.list`` methods not preserving the original :class:`Index`. (:issue:`58425`)

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

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

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

0 commit comments

Comments
 (0)