Repository: pandas-dev/pandas

Issue:
IndexError in repr of series objects containing complex numbers with negative imaginary parts
#### Code Sample, a copy-pastable example if possible

```python
from pandas import Series

print(Series([-1j]))

```
#### Problem description

This raises the following error:

```
Traceback (most recent call last):
  File "foo.py", line 3, in <module>
    print(Series([-1j]))
  File "/home/david/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pandas/core/series.py", line 1611, in __repr__
    length=show_dimensions,
  File "/home/david/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pandas/core/series.py", line 1677, in to_string
    result = formatter.to_string()
  File "/home/david/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pandas/io/formats/format.py", line 312, in to_string
    fmt_values = self._get_formatted_values()
  File "/home/david/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pandas/io/formats/format.py", line 299, in _get_formatted_values
    values_to_format, None, float_format=self.float_format, na_rep=self.na_rep
  File "/home/david/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pandas/io/formats/format.py", line 1032, in format_array
    return fmt_obj.get_result()
  File "/home/david/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pandas/io/formats/format.py", line 1063, in get_result
    fmt_values = self._format_strings()
  File "/home/david/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pandas/io/formats/format.py", line 1288, in _format_strings
    return list(self.get_result_as_array())
  File "/home/david/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pandas/io/formats/format.py", line 1252, in get_result_as_array
    formatted_values = format_values_with(float_format)
  File "/home/david/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pandas/io/formats/format.py", line 1234, in format_values_with
    return _trim_zeros_complex(values, na_rep)
  File "/home/david/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pandas/io/formats/format.py", line 1597, in _trim_zeros_complex
    return ["".join(separate_and_trim(x, na_rep)) for x in str_complexes]
  File "/home/david/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pandas/io/formats/format.py", line 1597, in <listcomp>
    return ["".join(separate_and_trim(x, na_rep)) for x in str_complexes]
  File "/home/david/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pandas/io/formats/format.py", line 1594, in separate_and_trim
    + ["j"]
IndexError: list index out of range
```

#### Expected Output

This should print something like the following:

```
0    0.0-1.0j
dtype: complex128
```

#### Output of ``pd.show_versions()``

<details>

[paste the output of ``pd.show_versions()`` here below this line]

INSTALLED VERSIONS
------------------
commit           : None
python           : 3.7.4.final.0
python-bits      : 64
OS               : Linux
OS-release       : 4.4.0-17134-Microsoft
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 0.25.0
numpy            : 1.16.4
pytz             : 2019.1
dateutil         : 2.8.0
pip              : 19.0.3
setuptools       : 40.8.0
Cython           : None
pytest           : 5.0.1
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : None
IPython          : 7.6.1
pandas_datareader: None
bs4              : None
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : None
matplotlib       : None
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pytables         : None
s3fs             : None
scipy            : None
sqlalchemy       : None
tables           : None
xarray           : None
xlrd             : None
xlwt             : None
xlsxwriter       : None
</details>



Discussion:
PR https://github.com/pandas-dev/pandas/pull/25745 is probably the culprit for this regression. Investigation and PR's welcome!

Resolving patch:
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -2593,12 +2593,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
--- 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 typing import (
     TYPE_CHECKING,
@@ -1688,17 +1689,10 @@ def _trim_zeros_complex(str_complexes: ndarray, na_rep: str = "NaN") -> List[str
     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(


Test patch:
diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py
--- 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