Pandas升级至2.1.1后pivot_table报TypeError问题求助
Pandas升级后TypeError问题排查与解决
问题背景
将Pandas从1.3.2版本升级到2.1.1版本后,以下代码运行时抛出TypeError: super(type, obj): obj must be an instance or subtype of type错误:
报错代码片段
import pandas as pd d = {'value': [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], 'x_axis_date': ['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08', '2020-01-09', '2020-01-10'], 'curve_year': [2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019]} df = pd.DataFrame(data=d) df = df.assign(x_axis_date=pd.to_datetime(df['x_axis_date'])) # dtypes: float64, datetime64[ns], int64 pv_aggregate_agg = pd.pivot_table(df, index=df.x_axis_date, values=['value'], aggfunc=pd.DataFrame.mean) # error here print(pv_aggregate_agg)
堆栈跟踪
Traceback (most recent call last): File "E:\repos\plot_graphs\venv\lib\site-packages\pandas\core\groupby\generic.py", line 1495, in aggregate result = gba.agg() File "E:\repos\plot_graphs\venv\lib\site-packages\pandas\core\apply.py", line 178, in agg return self.agg_list_like() File "E:\repos\plot_graphs\venv\lib\site-packages\pandas\core\apply.py", line 311, in agg_list_like return self.agg_or_apply_list_like(op_name="agg") File "E:\repos\plot_graphs\venv\lib\site-packages\pandas\core\apply.py", line 1351, in agg_or_apply_list_like keys, results = self.compute_list_like(op_name, selected_obj, kwargs) File "E:\repos\plot_graphs\venv\lib\site-packages\pandas\core\apply.py", line 370, in compute_list_like new_res = getattr(colg, op_name)(func, *args, **kwargs) File "E:\repos\plot_graphs\venv\lib\site-packages\pandas\core\groupby\generic.py", line 255, in aggregate ret = self._aggregate_multiple_funcs(func, *args, **kwargs) File "E:\repos\plot_graphs\venv\lib\site-packages\pandas\core\groupby\generic.py", line 360, in _aggregate_multiple_funcs results[key] = self.aggregate(func, *args, **kwargs) File "E:\repos\plot_graphs\venv\lib\site-packages\pandas\core\groupby\generic.py", line 292, in aggregate return self._python_agg_general(func, *args, **kwargs) File "E:\repos\plot_graphs\venv\lib\site-packages\pandas\core\groupby\generic.py", line 325, in _python_agg_general result = self.grouper.agg_series(obj, f) File "E:\repos\plot_graphs\venv\lib\site-packages\pandas\core\groupby\ops.py", line 850, in agg_series result = self._aggregate_series_pure_python(obj, func) File "E:\repos\plot_graphs\venv\lib\site-packages\pandas\core\groupby\ops.py", line 871, in _aggregate_series_pure_python res = func(group) File "E:\repos\plot_graphs\venv\lib\site-packages\pandas\core\groupby\generic.py", line 322, in <lambda> f = lambda x: func(x, *args, **kwargs) File "E:\repos\plot_graphs\venv\lib\site-packages\pandas\core\frame.py", line 11335, in mean result = super().mean(axis, skipna, numeric_only, **kwargs) TypeError: super(type, obj): obj must be an instance or subtype of type
原因分析
报错根源在于pd.pivot_table的aggfunc参数使用了pd.DataFrame.mean:
- 当
values参数指定单列(如这里的['value'])时,pivot_table会对每个分组的Series对象应用聚合函数 pd.DataFrame.mean是DataFrame类的方法,内部通过super()调用父类方法时,要求调用对象必须是DataFrame实例或其子类- Pandas 1.3.2对这种类型不匹配的情况兼容性较强,而2.1.1版本严格了类型检查,因此触发错误
解决方法
方法1:使用Series的mean方法(推荐)
将aggfunc改为pd.Series.mean,或者更简洁的字符串'mean':
# 写法1:使用Series.mean pv_aggregate_agg = pd.pivot_table(df, index=df.x_axis_date, values=['value'], aggfunc=pd.Series.mean) # 写法2:直接用字符串'mean'(更简洁,pandas会自动匹配对应方法) pv_aggregate_agg = pd.pivot_table(df, index=df.x_axis_date, values=['value'], aggfunc='mean')
方法2:自定义兼容DataFrame的聚合函数(适用于多列场景)
如果需要处理多列聚合,确保函数能接收DataFrame对象:
def custom_mean(df): return df.mean() pv_aggregate_agg = pd.pivot_table(df, index=df.x_axis_date, values=['value'], aggfunc=custom_mean)
修改后运行结果
两种方法都能正常输出预期结果:
value x_axis_date 2020-01-01 1.0 2020-01-02 1.0 2020-01-03 1.0 2020-01-04 1.0 2020-01-05 1.0 2020-01-06 1.0 2020-01-07 1.0 2020-01-08 1.0 2020-01-09 1.0 2020-01-10 1.0
内容的提问来源于stack exchange,提问作者Vitamin C
相关产品推荐
相关产品推荐

