-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Misleading error message when incorrectly using DataFrame.style.apply #45313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Not sure your right here, since in docs (should probably insert the word 'return' though):
The
and you are returning a list of correct length which is fine. However, you have a valid point about the error message. When you call Just as an example where this message is useful: def my_styler_error(s):
return "background-color: #E6E6E6" * len(s)
df = pd.DataFrame([[1, 2], [3, 4]], columns=["A", "B"])
df.style.apply(my_styler_error, subset="A") # <- ValueError: collapse to series: returns scalar
def my_styler_correct(s):
return ["background-color: #E6E6E6"] * len(s)
df = pd.DataFrame([[1, 2], [3, 4]], columns=["A", "B"])
df.style.apply(my_styler_correct, subset="A") # <- OK |
Ah, I see. To be honest I replaced the |
I had another short look: My initial example could be rewritten as: def my_styler_correct(s):
return ["background-color: #E6E6E6"] * len(s)
df = pd.DataFrame([[1, 2], [3, 4]], columns=["A", "B"])
df.style.apply(my_styler_correct, subset=[False, False])
# or
df.style.apply(my_styler_correct, subset=[]) I guess you have something like the diff below in mind? I can confirm this helps with the issue. diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py
index c8f65c6a2b..b2d70d2a0f 100644
--- a/pandas/io/formats/style.py
+++ b/pandas/io/formats/style.py
@@ -1487,6 +1487,9 @@ class Styler(StylerRenderer):
subset = slice(None) if subset is None else subset
subset = non_reducing_slice(subset)
data = self.data.loc[subset]
+ if data.empty:
+ return self
+
if axis is None:
result = func(data, **kwargs)
if not isinstance(result, DataFrame): |
yes you are right, although I wonder if |
Not sure - our original use case was actually If - for whatever reason, columns beeing dynamic in our case - the frame only contains the column C this is absolutely fine I'd say. |
Uh oh!
There was an error while loading. Please reload this page.
Reproducible Example
Issue Description
I was running into this issue when testing 1.4.0rc0 on a code base which worked with Pandas 1.3.
Our code which used to work before now triggers an error which is kind of expected when I read the documentation. The function passed to
DataFrame.style.apply
should either return a series or a data frame. In our case instead a list was returned. And after all, we should just useapplymap
.As this is clearly against the contract, it is ok, that the code now fails - it is just that the error message might be a bit misleading.
What happened in practice is that a list-like was returned. Returning a series would actually fix the problem.
Expected Behavior
Should the error message be changed to clarify that a series or data frame should be returned?
Should the first call - with a matching column - also fail?
Installed Versions
INSTALLED VERSIONS
commit : d023ba7
python : 3.9.6.final.0
python-bits : 64
OS : Darwin
OS-release : 20.6.0
Version : Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:31 PDT 2021; root:xnu-7195.141.2~5/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8
pandas : 1.4.0rc0
The text was updated successfully, but these errors were encountered: