Skip to content

Commit

Permalink
BUG: Fix formatting of complex numbers with exponents (pandas-dev#60417)
Browse files Browse the repository at this point in the history
Fix formatting of complex numbers with exponents
  • Loading branch information
snitish authored Nov 26, 2024
1 parent 0b6cece commit 759874e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
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

0 comments on commit 759874e

Please sign in to comment.