pandas :当DataFrame为空时,为什么DataFrame.apply(f,axis = 1)调用f? [英] Pandas: why does DataFrame.apply(f, axis=1) call f when the DataFrame is empty?

查看:67
本文介绍了 pandas :当DataFrame为空时,为什么DataFrame.apply(f,axis = 1)调用f?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么当DataFrame为空时,熊猫的DataFrame.apply方法调用正在应用的函数?

Why does Pandas' DataFrame.apply method call the function being applied when the DataFrame is empty?

例如:

>>> 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?

推荐答案

深入研究Pandas源代码,似乎是罪魁祸首:

Digging into the Pandas source, it looks like this is the culprit:

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()

Pandas似乎在不带参数的情况下调用该函数,以试图猜测结果是Series还是DataFrame.

It looks like Pandas is calling the function with no arguments in an attempt to guess whether the result should be a Series or a DataFrame.

我想有个补丁.

编辑:此问题已得到修复,现已记录在案,并允许使用reduce选项来避免此问题:

Edit: this issue has been patched, and is now both documented and allows the reduce option to be used to avoid it: http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.apply.html

这篇关于 pandas :当DataFrame为空时,为什么DataFrame.apply(f,axis = 1)调用f?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆