diff --git a/doc/source/whatsnew/v0.25.1.rst b/doc/source/whatsnew/v0.25.1.rst index 9007d1c06f197..169968314d70e 100644 --- a/doc/source/whatsnew/v0.25.1.rst +++ b/doc/source/whatsnew/v0.25.1.rst @@ -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`) - - diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c15f4ad8e1900..245e41ed16eb2 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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 diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 0e8ed7b25d665..11275973e2bb8 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -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 @@ -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"): diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 818bbc566aca8..ad47f714c9550 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -1537,7 +1537,7 @@ 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( { @@ -1545,6 +1545,7 @@ def test_to_string_complex_float_formatting(self): (0.4467846931321966 + 0.0715185102060818j), (0.2739442392974528 + 0.23515228785438969j), (0.26974928742135185 + 0.3250604054898979j), + (-1j), ] } ) @@ -1552,7 +1553,8 @@ def test_to_string_complex_float_formatting(self): 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