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 diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 87d4bbcc38373..6293ad6ae3ddf 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1486,7 +1486,9 @@ def _apply( subset = slice(None) if subset is None else subset subset = non_reducing_slice(subset) data = self.data.loc[subset] - if axis is None: + if data.empty: + result = DataFrame() + elif axis is None: result = func(data, **kwargs) if not isinstance(result, DataFrame): if not isinstance(result, np.ndarray): 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()