为什么Pandas的DataFrame.apply
方法在DataFrame
为空时调用正在应用的函数?
例如:
>>> 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?
答案 0 :(得分:3)
深入了解熊猫来源,看起来这就是罪魁祸首:
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
。
我想补丁是有序的。
修改:此问题已修补,现已记录在案,并允许reduce
选项用于避免此问题:http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.apply.html