Search code examples
pythonpandasdataframepivot-table

Pandas.pivot TypeError: super(type, obj): obj must be an instance or subtype of type


I have some code that stopped working after updating pandas from 1.3.2 to 2.1.1. I do not understand why and how to solve it, as the error is unclear to me. Help me figure it.

Snippet of my code:

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)

Stack trace:

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

Solution

  • Use 'mean' or np.mean instead of pd.DataFrame.mean:

    pv_aggregate_agg = pd.pivot_table(df, index=df.x_axis_date, values=['value'], 
                                      aggfunc='mean')
    print(pv_aggregate_agg)
    
    # Ouput
                 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
    

    Edit: However, you can also replace pd.DataFrame.mean with pd.Series.mean. Indeed, the aggfunc will be called for each group with a Series.