Skip to content

Commit 2d7af8d

Browse files
mroeschkeMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR pandas-dev#27511: BUG: display.precision of negative complex numbers
1 parent 4f6be8f commit 2d7af8d

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
@@ -2601,12 +2601,12 @@ def memory_usage(self, index=True, deep=False):
26012601
... for t in dtypes])
26022602
>>> df = pd.DataFrame(data)
26032603
>>> df.head()
2604-
int64 float64 complex128 object bool
2605-
0 1 1.0 1.0+0.0j 1 True
2606-
1 1 1.0 1.0+0.0j 1 True
2607-
2 1 1.0 1.0+0.0j 1 True
2608-
3 1 1.0 1.0+0.0j 1 True
2609-
4 1 1.0 1.0+0.0j 1 True
2604+
int64 float64 complex128 object bool
2605+
0 1 1.0 1.000000+0.000000j 1 True
2606+
1 1 1.0 1.000000+0.000000j 1 True
2607+
2 1 1.0 1.000000+0.000000j 1 True
2608+
3 1 1.0 1.000000+0.000000j 1 True
2609+
4 1 1.0 1.000000+0.000000j 1 True
26102610
26112611
>>> df.memory_usage()
26122612
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 unicodedata import east_asian_width
1011

@@ -1584,17 +1585,10 @@ def _trim_zeros_complex(str_complexes, na_rep="NaN"):
15841585
Separates the real and imaginary parts from the complex number, and
15851586
executes the _trim_zeros_float method on each of those.
15861587
"""
1587-
1588-
def separate_and_trim(str_complex, na_rep):
1589-
num_arr = str_complex.split("+")
1590-
return (
1591-
_trim_zeros_float([num_arr[0]], na_rep)
1592-
+ ["+"]
1593-
+ _trim_zeros_float([num_arr[1][:-1]], na_rep)
1594-
+ ["j"]
1595-
)
1596-
1597-
return ["".join(separate_and_trim(x, na_rep)) for x in str_complexes]
1588+
return [
1589+
"".join(_trim_zeros_float(re.split(r"([j+-])", x), na_rep))
1590+
for x in str_complexes
1591+
]
15981592

15991593

16001594
def _trim_zeros_float(str_floats, na_rep="NaN"):

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)