Skip to content

Backport PR #27511 on branch 0.25.x (BUG: display.precision of negative complex numbers) #27587

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

Merged
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/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Timezones
Numeric
^^^^^^^
- Bug in :meth:`Series.interpolate` when using a timezone aware :class:`DatetimeIndex` (:issue:`27548`)
- Bug when printing negative floating point complex numbers would raise an ``IndexError`` (:issue:`27484`)
-
-

Expand Down
12 changes: 6 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2601,12 +2601,12 @@ def memory_usage(self, index=True, deep=False):
... for t in dtypes])
>>> df = pd.DataFrame(data)
>>> df.head()
int64 float64 complex128 object bool
0 1 1.0 1.0+0.0j 1 True
1 1 1.0 1.0+0.0j 1 True
2 1 1.0 1.0+0.0j 1 True
3 1 1.0 1.0+0.0j 1 True
4 1 1.0 1.0+0.0j 1 True
int64 float64 complex128 object bool
0 1 1.0 1.000000+0.000000j 1 True
1 1 1.0 1.000000+0.000000j 1 True
2 1 1.0 1.000000+0.000000j 1 True
3 1 1.0 1.000000+0.000000j 1 True
4 1 1.0 1.000000+0.000000j 1 True

>>> df.memory_usage()
Index 128
Expand Down
16 changes: 5 additions & 11 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from functools import partial
from io import StringIO
import re
from shutil import get_terminal_size
from unicodedata import east_asian_width

Expand Down Expand Up @@ -1584,17 +1585,10 @@ def _trim_zeros_complex(str_complexes, na_rep="NaN"):
Separates the real and imaginary parts from the complex number, and
executes the _trim_zeros_float method on each of those.
"""

def separate_and_trim(str_complex, na_rep):
num_arr = str_complex.split("+")
return (
_trim_zeros_float([num_arr[0]], na_rep)
+ ["+"]
+ _trim_zeros_float([num_arr[1][:-1]], na_rep)
+ ["j"]
)

return ["".join(separate_and_trim(x, na_rep)) for x in str_complexes]
return [
"".join(_trim_zeros_float(re.split(r"([j+-])", x), na_rep))
for x in str_complexes
]


def _trim_zeros_float(str_floats, na_rep="NaN"):
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1537,22 +1537,24 @@ def test_to_string_float_index(self):
assert result == expected

def test_to_string_complex_float_formatting(self):
# GH #25514
# GH #25514, 25745
with pd.option_context("display.precision", 5):
df = DataFrame(
{
"x": [
(0.4467846931321966 + 0.0715185102060818j),
(0.2739442392974528 + 0.23515228785438969j),
(0.26974928742135185 + 0.3250604054898979j),
(-1j),
]
}
)
result = df.to_string()
expected = (
" x\n0 0.44678+0.07152j\n"
"1 0.27394+0.23515j\n"
"2 0.26975+0.32506j"
"2 0.26975+0.32506j\n"
"3 -0.00000-1.00000j"
)
assert result == expected

Expand Down