From 791a00a27da8b1a85c213d887e823fb3f9e4a442 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sun, 21 Jul 2019 17:57:28 -0700 Subject: [PATCH 1/3] BUG: display.precision of negative complex numbers --- doc/source/whatsnew/v0.25.1.rst | 2 +- pandas/io/formats/format.py | 16 +++++----------- pandas/tests/io/formats/test_format.py | 6 ++++-- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/doc/source/whatsnew/v0.25.1.rst b/doc/source/whatsnew/v0.25.1.rst index 6234bc0f7bd35..a92b9b9cb71e7 100644 --- a/doc/source/whatsnew/v0.25.1.rst +++ b/doc/source/whatsnew/v0.25.1.rst @@ -56,7 +56,7 @@ Timezones Numeric ^^^^^^^ -- +- Bug in :class:`format` in which negative floating point complex numbers would raise an ``IndexError`` (:issue:`27484`) - - diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 11b69064723c5..0c66496065a94 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 typing import TYPE_CHECKING, List, Optional, TextIO, Tuple, Union, cast from unicodedata import east_asian_width @@ -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"): 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 From cceba7860f05ce7da1b1786be5f0cff574aa2be6 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sun, 21 Jul 2019 20:27:18 -0700 Subject: [PATCH 2/3] Doctest --- pandas/core/frame.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 8fdfc960603c6..97a05491926e5 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 From ed52a745e848ce2062b091f79b6e9e32de6d4138 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sun, 21 Jul 2019 23:06:15 -0700 Subject: [PATCH 3/3] Clarify whatsnew note --- doc/source/whatsnew/v0.25.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.1.rst b/doc/source/whatsnew/v0.25.1.rst index a92b9b9cb71e7..6e1d4ed1ee43c 100644 --- a/doc/source/whatsnew/v0.25.1.rst +++ b/doc/source/whatsnew/v0.25.1.rst @@ -56,7 +56,7 @@ Timezones Numeric ^^^^^^^ -- Bug in :class:`format` in which negative floating point complex numbers would raise an ``IndexError`` (:issue:`27484`) +- Bug when printing negative floating point complex numbers would raise an ``IndexError`` (:issue:`27484`) - -