From aa602098969bc939b93bfa55b2c584fd252bd1bb Mon Sep 17 00:00:00 2001 From: Gim Seng Ng Date: Fri, 10 Jul 2020 14:09:56 +0100 Subject: [PATCH 1/5] fix precision display issue relating to trimming zeros --- pandas/io/formats/format.py | 17 +++++++++++------ pandas/tests/io/formats/test_format.py | 9 +++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 22cdd8e235e0b..27df014620f56 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1382,9 +1382,9 @@ def format_values_with(float_format): if self.fixed_width: if is_complex: - result = _trim_zeros_complex(values, na_rep) + result = _trim_zeros_complex(values, self.decimal, na_rep) else: - result = _trim_zeros_float(values, na_rep) + result = _trim_zeros_float(values, self.decimal, na_rep) return np.asarray(result, dtype="object") return values @@ -1754,19 +1754,21 @@ def just(x): return result -def _trim_zeros_complex(str_complexes: np.ndarray, na_rep: str = "NaN") -> List[str]: +def _trim_zeros_complex( + str_complexes: np.ndarray, decimal: str = ".", 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. """ return [ - "".join(_trim_zeros_float(re.split(r"([j+-])", x), na_rep)) + "".join(_trim_zeros_float(re.split(r"([j+-])", x), decimal, na_rep)) for x in str_complexes ] def _trim_zeros_float( - str_floats: Union[np.ndarray, List[str]], na_rep: str = "NaN" + str_floats: Union[np.ndarray, List[str]], decimal: str = ".", na_rep: str = "NaN" ) -> List[str]: """ Trims zeros, leaving just one before the decimal points if need be. @@ -1778,8 +1780,11 @@ def _is_number(x): def _cond(values): finite = [x for x in values if _is_number(x)] + has_decimal = [decimal in x for x in finite] + return ( len(finite) > 0 + and all(has_decimal) and all(x.endswith("0") for x in finite) and not (any(("e" in x) or ("E" in x) for x in finite)) ) @@ -1788,7 +1793,7 @@ def _cond(values): trimmed = [x[:-1] if _is_number(x) else x for x in trimmed] # leave one 0 after the decimal points if need be. - return [x + "0" if x.endswith(".") and _is_number(x) else x for x in trimmed] + return [x + "0" if x.endswith(decimal) and _is_number(x) else x for x in trimmed] def _has_names(index: Index) -> bool: diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 3c40a2ae8d6b8..bbd2d274ea625 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -2910,6 +2910,15 @@ def test_format(self): assert result[0] == " 12.0" assert result[1] == " 0.0" + def test_output_display_precision_trailing_zeroes(self): + # Issue #20359: trimming zeros while there is no decimal point + + # Happens when display precision is set to zero + with pd.option_context("display.precision", 0): + s = pd.Series([840.0, 4200.0]) + expected_output = "0 840\n" "1 4200\n" "dtype: float64" + assert str(s) == expected_output + def test_output_significant_digits(self): # Issue #9764 From 52fa1796a5aadd5ff14d698d70e6e17b9fe7caa4 Mon Sep 17 00:00:00 2001 From: Gim Seng Ng Date: Fri, 10 Jul 2020 14:41:22 +0100 Subject: [PATCH 2/5] fix split string lint issue --- pandas/tests/io/formats/test_format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index bbd2d274ea625..e236b3da73c69 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -2916,7 +2916,7 @@ def test_output_display_precision_trailing_zeroes(self): # Happens when display precision is set to zero with pd.option_context("display.precision", 0): s = pd.Series([840.0, 4200.0]) - expected_output = "0 840\n" "1 4200\n" "dtype: float64" + expected_output = "0 840\n1 4200\ndtype: float64" assert str(s) == expected_output def test_output_significant_digits(self): From 994fcd5139e82940cca01ace648a3c5710f460cb Mon Sep 17 00:00:00 2001 From: Gim Seng Ng Date: Fri, 10 Jul 2020 16:17:54 +0100 Subject: [PATCH 3/5] added bug description in whats new --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 5dff6d729479a..975887696fa68 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -1018,6 +1018,7 @@ MultiIndex I/O ^^^ +- Bug in print-out when precision option is zero. (:issue:`20359`) - Bug in :meth:`read_json` where integer overflow was occurring when json contains big number strings. (:issue:`30320`) - `read_csv` will now raise a ``ValueError`` when the arguments `header` and `prefix` both are not `None`. (:issue:`27394`) - Bug in :meth:`DataFrame.to_json` was raising ``NotFoundError`` when ``path_or_buf`` was an S3 URI (:issue:`28375`) From e330ea6bb71c560dca8ff70669591d7c64befb1f Mon Sep 17 00:00:00 2001 From: Gim Seng Ng Date: Fri, 10 Jul 2020 16:26:56 +0100 Subject: [PATCH 4/5] added display.precision in whats new --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 975887696fa68..d2723a5122ab5 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -1018,7 +1018,7 @@ MultiIndex I/O ^^^ -- Bug in print-out when precision option is zero. (:issue:`20359`) +- Bug in print-out when `display.precision` is zero. (:issue:`20359`) - Bug in :meth:`read_json` where integer overflow was occurring when json contains big number strings. (:issue:`30320`) - `read_csv` will now raise a ``ValueError`` when the arguments `header` and `prefix` both are not `None`. (:issue:`27394`) - Bug in :meth:`DataFrame.to_json` was raising ``NotFoundError`` when ``path_or_buf`` was an S3 URI (:issue:`28375`) From 84f3aef8298d78e6f348a4afc9f793c4d9727cb4 Mon Sep 17 00:00:00 2001 From: Gim Seng Ng Date: Fri, 10 Jul 2020 16:27:32 +0100 Subject: [PATCH 5/5] corrected single tick to double tick --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index d2723a5122ab5..bff3e2fcdb267 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -1018,7 +1018,7 @@ MultiIndex I/O ^^^ -- Bug in print-out when `display.precision` is zero. (:issue:`20359`) +- Bug in print-out when ``display.precision`` is zero. (:issue:`20359`) - Bug in :meth:`read_json` where integer overflow was occurring when json contains big number strings. (:issue:`30320`) - `read_csv` will now raise a ``ValueError`` when the arguments `header` and `prefix` both are not `None`. (:issue:`27394`) - Bug in :meth:`DataFrame.to_json` was raising ``NotFoundError`` when ``path_or_buf`` was an S3 URI (:issue:`28375`)