Skip to content

Commit ba31de3

Browse files
mroeschkeWillAyd
authored andcommitted
BUG: display.precision of negative complex numbers (#27511)
1 parent 3b96ada commit ba31de3

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

doc/source/whatsnew/v0.25.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Timezones
5757
Numeric
5858
^^^^^^^
5959
- Bug in :meth:`Series.interpolate` when using a timezone aware :class:`DatetimeIndex` (:issue:`27548`)
60+
- Bug when printing negative floating point complex numbers would raise an ``IndexError`` (:issue:`27484`)
6061
-
6162
-
6263

pandas/core/frame.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -2593,12 +2593,12 @@ def memory_usage(self, index=True, deep=False):
25932593
... for t in dtypes])
25942594
>>> df = pd.DataFrame(data)
25952595
>>> df.head()
2596-
int64 float64 complex128 object bool
2597-
0 1 1.0 1.0+0.0j 1 True
2598-
1 1 1.0 1.0+0.0j 1 True
2599-
2 1 1.0 1.0+0.0j 1 True
2600-
3 1 1.0 1.0+0.0j 1 True
2601-
4 1 1.0 1.0+0.0j 1 True
2596+
int64 float64 complex128 object bool
2597+
0 1 1.0 1.000000+0.000000j 1 True
2598+
1 1 1.0 1.000000+0.000000j 1 True
2599+
2 1 1.0 1.000000+0.000000j 1 True
2600+
3 1 1.0 1.000000+0.000000j 1 True
2601+
4 1 1.0 1.000000+0.000000j 1 True
26022602
26032603
>>> df.memory_usage()
26042604
Index 128

pandas/io/formats/format.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from functools import partial
77
from io import StringIO
8+
import re
89
from shutil import get_terminal_size
910
from typing import (
1011
TYPE_CHECKING,
@@ -1688,17 +1689,10 @@ def _trim_zeros_complex(str_complexes: ndarray, na_rep: str = "NaN") -> List[str
16881689
Separates the real and imaginary parts from the complex number, and
16891690
executes the _trim_zeros_float method on each of those.
16901691
"""
1691-
1692-
def separate_and_trim(str_complex, na_rep):
1693-
num_arr = str_complex.split("+")
1694-
return (
1695-
_trim_zeros_float([num_arr[0]], na_rep)
1696-
+ ["+"]
1697-
+ _trim_zeros_float([num_arr[1][:-1]], na_rep)
1698-
+ ["j"]
1699-
)
1700-
1701-
return ["".join(separate_and_trim(x, na_rep)) for x in str_complexes]
1692+
return [
1693+
"".join(_trim_zeros_float(re.split(r"([j+-])", x), na_rep))
1694+
for x in str_complexes
1695+
]
17021696

17031697

17041698
def _trim_zeros_float(

pandas/tests/io/formats/test_format.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1537,22 +1537,24 @@ def test_to_string_float_index(self):
15371537
assert result == expected
15381538

15391539
def test_to_string_complex_float_formatting(self):
1540-
# GH #25514
1540+
# GH #25514, 25745
15411541
with pd.option_context("display.precision", 5):
15421542
df = DataFrame(
15431543
{
15441544
"x": [
15451545
(0.4467846931321966 + 0.0715185102060818j),
15461546
(0.2739442392974528 + 0.23515228785438969j),
15471547
(0.26974928742135185 + 0.3250604054898979j),
1548+
(-1j),
15481549
]
15491550
}
15501551
)
15511552
result = df.to_string()
15521553
expected = (
15531554
" x\n0 0.44678+0.07152j\n"
15541555
"1 0.27394+0.23515j\n"
1555-
"2 0.26975+0.32506j"
1556+
"2 0.26975+0.32506j\n"
1557+
"3 -0.00000-1.00000j"
15561558
)
15571559
assert result == expected
15581560

0 commit comments

Comments
 (0)