Skip to content

Commit d19a31e

Browse files
authored
BUG: no apply to empty df in Styler (#45383)
Co-authored-by: JHM Darbyshire (iMac) <[email protected]>
1 parent 4e034ec commit d19a31e

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

doc/source/whatsnew/v1.5.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Styler
2020
^^^^^^
2121

2222
- New method :meth:`.Styler.to_string` for alternative customisable output methods (:issue:`44502`)
23+
- Various bug fixes, see below.
2324

2425
.. _whatsnew_150.enhancements.enhancement2:
2526

@@ -258,7 +259,7 @@ ExtensionArray
258259

259260
Styler
260261
^^^^^^
261-
-
262+
- Minor bug when attempting to apply styling functions to an empty DataFrame subset (:issue:`45313`)
262263
-
263264

264265
Other

pandas/io/formats/style.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,9 @@ def _apply(
14861486
subset = slice(None) if subset is None else subset
14871487
subset = non_reducing_slice(subset)
14881488
data = self.data.loc[subset]
1489-
if axis is None:
1489+
if data.empty:
1490+
result = DataFrame()
1491+
elif axis is None:
14901492
result = func(data, **kwargs)
14911493
if not isinstance(result, DataFrame):
14921494
if not isinstance(result, np.ndarray):

pandas/tests/io/formats/style/test_style.py

+6
Original file line numberDiff line numberDiff line change
@@ -1548,3 +1548,9 @@ def test_col_trimming_hide_columns():
15481548
assert ctx["head"][0][c + 2]["is_visible"] == vals[1]
15491549

15501550
assert len(ctx["body"][0]) == 6 # index + 2 hidden + 2 visible + trimming col
1551+
1552+
1553+
def test_no_empty_apply(mi_styler):
1554+
# 45313
1555+
mi_styler.apply(lambda s: ["a:v;"] * 2, subset=[False, False])
1556+
mi_styler._compute()

0 commit comments

Comments
 (0)