Pandas: why does DataFrame.apply (f, axis = 1) call f when the DataFrame is empty?

Why DataFrame.applydoes the Pandas' method call the function to use when DataFrameempty?

For example:

>>> import pandas as pd
>>> df = pd.DataFrame({"foo": []})
>>> df
Empty DataFrame
Columns: [foo]
Index: []
>>> x = []
>>> df.apply(x.append, axis=1)
Series([], dtype: float64)
>>> x
[Series([], dtype: float64)] # <<< why was the apply callback called with an empty row?
+4
source share
1 answer

Delving into a Pandas source, it looks like it's a criminal:

if not all(self.shape):
    # How to determine this better?
    is_reduction = False
    try:
        is_reduction = not isinstance(f(_EMPTY_SERIES), Series)
    except Exception:
        pass

    if is_reduction:
        return Series(NA, index=self._get_agg_axis(axis))
    else:
        return self.copy()

It seems that Pandas calls the function with no arguments in an attempt to guess whether the result should be Seriesor DataFrame.

I guess the patch is fine.

Edit : this problem has been fixed, and now it is documented and allows you to use the parameter reduceto avoid this: http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.apply.html

+3

All Articles