From 0f3b2e4e109a443ea01375c1156a620c7aa18601 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 15 Jan 2022 09:36:33 +0100 Subject: [PATCH 1/3] whats new --- doc/source/whatsnew/v1.4.0.rst | 1 + pandas/io/formats/style.py | 2 ++ pandas/tests/io/formats/style/test_style.py | 6 ++++++ 3 files changed, 9 insertions(+) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 64d8e36177051..5ac3334bff471 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -973,6 +973,7 @@ Styler - Bug when combining non-sparse rendering and :meth:`.Styler.hide_columns` or :meth:`.Styler.hide_index` (:issue:`43464`) - Bug setting a table style when using multiple selectors in :class:`.Styler` (:issue:`44011`) - Bugs where row trimming and column trimming failed to reflect hidden rows (:issue:`43703`, :issue:`44247`) +- Minor bug when attempting to apply styling functions to an empty DataFrame subset (:issue:`45313`) Other ^^^^^ diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 87d4bbcc38373..8c344fb6311e4 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1486,6 +1486,8 @@ def _apply( subset = slice(None) if subset is None else subset subset = non_reducing_slice(subset) data = self.data.loc[subset] + if data.empty: + return self # don't use DataFrame.apply on empty df if axis is None: result = func(data, **kwargs) if not isinstance(result, DataFrame): diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index cb01cc39250b5..915497e614b3a 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1548,3 +1548,9 @@ def test_col_trimming_hide_columns(): assert ctx["head"][0][c + 2]["is_visible"] == vals[1] assert len(ctx["body"][0]) == 6 # index + 2 hidden + 2 visible + trimming col + + +def test_no_empty_apply(mi_styler): + # 45313 + mi_styler.apply(lambda s: ["a:v;"] * 2, subset=[False, False]) + mi_styler._compute() From f81a376bd96723da1b3fe316e2e20a2d54812fc7 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 16 Jan 2022 18:22:32 +0100 Subject: [PATCH 2/3] doc string replacement --- doc/source/whatsnew/v1.4.0.rst | 1 - doc/source/whatsnew/v1.5.0.rst | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 5ac3334bff471..64d8e36177051 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -973,7 +973,6 @@ Styler - Bug when combining non-sparse rendering and :meth:`.Styler.hide_columns` or :meth:`.Styler.hide_index` (:issue:`43464`) - Bug setting a table style when using multiple selectors in :class:`.Styler` (:issue:`44011`) - Bugs where row trimming and column trimming failed to reflect hidden rows (:issue:`43703`, :issue:`44247`) -- Minor bug when attempting to apply styling functions to an empty DataFrame subset (:issue:`45313`) Other ^^^^^ diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 9a6455d4d012f..ad28b2f918ba4 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -20,6 +20,7 @@ Styler ^^^^^^ - New method :meth:`.Styler.to_string` for alternative customisable output methods (:issue:`44502`) + - Various bug fixes, see below. .. _whatsnew_150.enhancements.enhancement2: @@ -206,7 +207,7 @@ ExtensionArray Styler ^^^^^^ -- +- Minor bug when attempting to apply styling functions to an empty DataFrame subset (:issue:`45313`) - Other From 8a5ca7bddc0224f78abd1634f65836ca7701fde7 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 16 Jan 2022 18:31:14 +0100 Subject: [PATCH 3/3] use existing logic --- pandas/io/formats/style.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 8c344fb6311e4..6293ad6ae3ddf 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1487,8 +1487,8 @@ def _apply( subset = non_reducing_slice(subset) data = self.data.loc[subset] if data.empty: - return self # don't use DataFrame.apply on empty df - if axis is None: + result = DataFrame() + elif axis is None: result = func(data, **kwargs) if not isinstance(result, DataFrame): if not isinstance(result, np.ndarray):