A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pandas-dev/pandas/issues/45313 below:

Misleading error message when incorrectly using DataFrame.style.apply · Issue #45313 · pandas-dev/pandas · GitHub

Reproducible Example
def my_styler(df: pd.DataFrame):
    # This would silence the error message
    # return pd.Series("background-color: #E6E6E6" if c else "" for c in df)

    # The docs tell us to return a data frame/series here
    return ["background-color: #E6E6E6" if c else "" for c in df]

df = pd.DataFrame([[1, 2], [3, 4]], columns=["A", "B"])

# Not ok according to the docs, but it does not fail
df.style.apply(my_styler, subset=df.columns == "A").to_excel("foo.xlsx")

# Subset is empty, my_styler is called with an empty series, things fail
df.style.apply(my_styler, subset=df.columns == "C").to_excel("foo.xlsx")
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 use applymap.

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.

        if isinstance(result, Series):
>           raise ValueError(
                f"Function {repr(func)} resulted in the apply method collapsing to a "
                f"Series.\nUsually, this is the result of the function returning a "
                f"single value, instead of list-like."
            )
E           ValueError: Function <function test_xlsx.<locals>.my_styler at 0x7fe1d9baea60> resulted in the apply method collapsing to a Series.
E           Usually, this is the result of the function returning a single value, instead of list-like.

/Users/armin/venv/weplan3.9/lib/python3.9/site-packages/pandas/io/formats/style.py:1310: ValueError

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4