[英]`groupby` column not passed to `apply` function. Possible bug in Pandas?
Possible bug in GroupBy.apply
(pandas 0.25.3): The following code makes a groupby
by the class
column, and the applied function tries to access the class
column. GroupBy.apply
(pandas 0.25.3) 中可能存在的错误:以下代码通过class
列创建groupby
,应用函数尝试访问class
列。 The code produces a KeyError: 'class'
exception:代码产生一个
KeyError: 'class'
异常:
import pandas as pd
df = pd.DataFrame(
[
("bird", "Falconiformes", 389.0),
("bird", "Psittaciformes", 24.0),
("mammal", "Carnivora", 80.2),
("mammal", "Primates", 20),
("mammal", "Carnivora", 58),
],
index=["falcon", "parrot", "lion", "monkey", "leopard"],
columns=("class", "order", "max_speed"),
)
class_to_features = {"bird": ["wings", "feathers", "beak"], "mammal": ["udder"]}
def exec_groupby(df, _temp, c_2_f=None):
def _helper(df):
if c_2_f is not None:
return c_2_f[df["class"].iloc[0]] + _temp # KeyError "class"
else:
return "goo" + _temp
return df.groupby(["class"]).apply(lambda df: _helper(df))
print(exec_groupby(df, "foo"))
print(exec_groupby(df, "foo", class_to_features))
However, if I remove the + _temp
from the return
statement it works fine!但是,如果我从
return
语句中删除+ _temp
它工作正常!
def exec_groupby(df, _temp, c_2_f=None):
def _helper(df):
if c_2_f is not None:
return c_2_f[df["class"].iloc[0]] # Works fine! no errors
else:
return "goo" + _temp
return df.groupby(["class"]).apply(lambda df: _helper(df))
It seems like a combination of c_2_f
not being None
and access the column that causes the exception.似乎
c_2_f
的组合不是None
并访问导致异常的列。 Am I missing something?我错过了什么吗?
The error log reads错误日志读取
TypeError: can only concatenate list (not "str") to list
...
During handling of the above exception, another exception occurred:
...
KeyError: 'class'
c_2_f
has keys which are lists, so you can't concatenate it with str
c_2_f
键是列表,所以你不能将它与str
连接起来
def exec_groupby(df, _temp, c_2_f=None):
def _helper(df):
if c_2_f is not None:
return c_2_f[df["class"].iloc[0]] + [_temp] # Works fine! no errors
else:
return "goo" + _temp
return df.groupby(["class"]).apply(lambda df: _helper(df))
This works fine and gives这工作正常,并给出
class
bird goofoo
mammal goofoo
dtype: object
class
bird [wings, feathers, beak, foo]
mammal [udder, foo]
dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.