Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Timezones

Numeric
^^^^^^^
-
- Bug in :class:`format` in which negative floating point complex numbers would raise an ``IndexError`` (:issue:`27484`)
Copy link
Member

Choose a reason for hiding this comment

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

Is this the right class?

-
-

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 typing import TYPE_CHECKING, List, Optional, TextIO, Tuple, Union, cast
from unicodedata import east_asian_width
Expand Down Expand Up @@ -1600,17 +1601,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