From 9f47776faeb1a7f9dfd86a5460554677ddaf16db Mon Sep 17 00:00:00 2001 From: Yash Gupta Date: Fri, 20 Aug 2021 12:55:58 +0530 Subject: [PATCH 1/2] Use _get_axis_number() in _apply() --- pandas/io/formats/style.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 81bd14629cfd3..368a30993497e 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1148,11 +1148,7 @@ def _apply( subset = slice(None) if subset is None else subset subset = non_reducing_slice(subset) data = self.data.loc[subset] - if axis in [0, "index"]: - result = data.apply(func, axis=0, **kwargs) - elif axis in [1, "columns"]: - result = data.T.apply(func, axis=0, **kwargs).T # see GH 42005 - else: + if axis is None: result = func(data, **kwargs) if not isinstance(result, DataFrame): if not isinstance(result, np.ndarray): @@ -1167,6 +1163,9 @@ def _apply( f"Expected shape: {data.shape}" ) result = DataFrame(result, index=data.index, columns=data.columns) + else: + axis = self.data._get_axis_number(axis) + result = data.T.apply(func, axis=0, **kwargs).T if axis == 1 else data.apply(func, axis=0, **kwargs) if isinstance(result, Series): raise ValueError( From 76801434e9c13f3e018ac467d15d3640d35742b3 Mon Sep 17 00:00:00 2001 From: Yash Gupta Date: Fri, 20 Aug 2021 14:07:47 +0530 Subject: [PATCH 2/2] Update _apply() Co-authored-by: attack68 <24256554+attack68@users.noreply.github.com> --- pandas/io/formats/style.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 368a30993497e..81b62572aff2d 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1165,7 +1165,10 @@ def _apply( result = DataFrame(result, index=data.index, columns=data.columns) else: axis = self.data._get_axis_number(axis) - result = data.T.apply(func, axis=0, **kwargs).T if axis == 1 else data.apply(func, axis=0, **kwargs) + if axis == 0: + result = data.apply(func, axis=0, **kwargs) + else: + result = data.T.apply(func, axis=0, **kwargs).T # see GH 42005 if isinstance(result, Series): raise ValueError(