From b3f5b7e727fe41b7e44ccd6b13360a53a05e6ec0 Mon Sep 17 00:00:00 2001 From: gabriellm1 Date: Tue, 27 Aug 2019 19:43:52 -0300 Subject: [PATCH 1/6] BUG: issue-25955 fixed --- doc/source/whatsnew/v1.0.0.rst | 1 + pandas/io/formats/format.py | 4 +++ .../formats/data/html/truncate_formatter.html | 36 +++++++++++++++++++ pandas/tests/io/formats/test_to_html.py | 16 +++++++++ 4 files changed, 57 insertions(+) create mode 100644 pandas/tests/io/formats/data/html/truncate_formatter.html diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 58918f2d8c40e..0ca75402c41ca 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -83,6 +83,7 @@ Performance improvements Bug fixes ~~~~~~~~~ +- Bug in to_html() when using formatters= and max_cols together fixed (:issue:`25955`) Categorical ^^^^^^^^^^^ diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 61af935bd8227..6857f971e68c4 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -656,6 +656,10 @@ def _chk_truncate(self) -> None: frame = concat( (frame.iloc[:, :col_num], frame.iloc[:, -col_num:]), axis=1 ) + # truncate formatter + if is_list_like(self.formatters) and self.formatters: + truncate_fmt = cast(List[Callable], self.formatters) + self.formatters = truncate_fmt[:col_num] + truncate_fmt[-col_num:] self.tr_col_num = col_num if truncate_v: # cast here since if truncate_v is True, max_rows_adj is not None diff --git a/pandas/tests/io/formats/data/html/truncate_formatter.html b/pandas/tests/io/formats/data/html/truncate_formatter.html new file mode 100644 index 0000000000000..71bdf43258234 --- /dev/null +++ b/pandas/tests/io/formats/data/html/truncate_formatter.html @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
A...D
01_mod...4
15_mod...8
29_mod...12
313_mod...16
\ No newline at end of file diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 448e869df950d..2e24f639cb3d9 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -235,6 +235,22 @@ def test_to_html_truncate(datapath): assert result == expected +def test_to_html_truncate_formatter(datapath): + data = [ + {"A": 1, "B": 2, "C": 3, "D": 4}, + {"A": 5, "B": 6, "C": 7, "D": 8}, + {"A": 9, "B": 10, "C": 11, "D": 12}, + {"A": 13, "B": 14, "C": 15, "D": 16}, + ] + + df = pd.DataFrame(data) + fmt = lambda x: str(x) + "_mod" + formatters = [fmt, fmt, None, None] + result = df.to_html(formatters=formatters, max_cols=3) + expected = expected_html(datapath, "truncate_formatter") + assert result == expected + + @pytest.mark.parametrize( "sparsify,expected", [(True, "truncate_multi_index"), (False, "truncate_multi_index_sparse_off")], From ec314fc713362a4f2ef6c89759c2ef58f2c8eda4 Mon Sep 17 00:00:00 2001 From: gabriellm1 Date: Thu, 29 Aug 2019 18:42:03 -0300 Subject: [PATCH 2/6] Update doc/source/whatsnew/v1.0.0.rst Co-Authored-By: Simon Hawkins --- doc/source/whatsnew/v1.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 0ca75402c41ca..7c02a6e72f0cb 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -83,7 +83,7 @@ Performance improvements Bug fixes ~~~~~~~~~ -- Bug in to_html() when using formatters= and max_cols together fixed (:issue:`25955`) +- Bug in :meth:`DataFrame.to_html` when using ``formatters=`` and ``max_cols`` together. (:issue:`25955`) Categorical ^^^^^^^^^^^ From cd84e8d54e546117c3170c748d959f4643bf05d4 Mon Sep 17 00:00:00 2001 From: gabriellm1 Date: Thu, 29 Aug 2019 18:53:37 -0300 Subject: [PATCH 3/6] review --- pandas/tests/io/formats/data/html/truncate_formatter.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/data/html/truncate_formatter.html b/pandas/tests/io/formats/data/html/truncate_formatter.html index 71bdf43258234..7615ef89d85d1 100644 --- a/pandas/tests/io/formats/data/html/truncate_formatter.html +++ b/pandas/tests/io/formats/data/html/truncate_formatter.html @@ -33,4 +33,4 @@ 16 - \ No newline at end of file + From 663de0d548f8eac6ba2e46c762407f2ce451ef20 Mon Sep 17 00:00:00 2001 From: gabriellm1 Date: Thu, 29 Aug 2019 18:54:33 -0300 Subject: [PATCH 4/6] review-2 --- pandas/tests/io/formats/test_to_html.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 2e24f639cb3d9..b5e816c0c0002 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -236,6 +236,7 @@ def test_to_html_truncate(datapath): def test_to_html_truncate_formatter(datapath): + #issue-25955 data = [ {"A": 1, "B": 2, "C": 3, "D": 4}, {"A": 5, "B": 6, "C": 7, "D": 8}, @@ -243,7 +244,7 @@ def test_to_html_truncate_formatter(datapath): {"A": 13, "B": 14, "C": 15, "D": 16}, ] - df = pd.DataFrame(data) + df = DataFrame(data) fmt = lambda x: str(x) + "_mod" formatters = [fmt, fmt, None, None] result = df.to_html(formatters=formatters, max_cols=3) From f7cca52c1d970cdb9f6d7cd6e67ccf517b236f87 Mon Sep 17 00:00:00 2001 From: gabriellm1 Date: Thu, 29 Aug 2019 18:58:20 -0300 Subject: [PATCH 5/6] pep8 correction --- pandas/tests/io/formats/test_to_html.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index b5e816c0c0002..b21f437f2b6e9 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -236,7 +236,7 @@ def test_to_html_truncate(datapath): def test_to_html_truncate_formatter(datapath): - #issue-25955 + # issue-25955 data = [ {"A": 1, "B": 2, "C": 3, "D": 4}, {"A": 5, "B": 6, "C": 7, "D": 8}, From a3af4a4c48c5f32ffe21d00930f10d9b148ebec9 Mon Sep 17 00:00:00 2001 From: gabriellm1 Date: Tue, 10 Sep 2019 21:07:53 -0300 Subject: [PATCH 6/6] removing cast --- pandas/io/formats/format.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 6857f971e68c4..fd1afcff1dce0 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -657,9 +657,12 @@ def _chk_truncate(self) -> None: (frame.iloc[:, :col_num], frame.iloc[:, -col_num:]), axis=1 ) # truncate formatter - if is_list_like(self.formatters) and self.formatters: - truncate_fmt = cast(List[Callable], self.formatters) - self.formatters = truncate_fmt[:col_num] + truncate_fmt[-col_num:] + if isinstance(self.formatters, (list, tuple)): + truncate_fmt = self.formatters + self.formatters = [ + *truncate_fmt[:col_num], + *truncate_fmt[-col_num:], + ] self.tr_col_num = col_num if truncate_v: # cast here since if truncate_v is True, max_rows_adj is not None