Skip to content

Commit

Permalink
[FIX] _builtin_table import in groupby apply (changed in pandas>=1.3.…
Browse files Browse the repository at this point in the history
…0) (#2184)

I have recently had an issue while upgrading pandas to the latest version in my Databricks environment :

`AttributeError: type object 'SelectionMixin' has no attribute '_builtin_table'`


Pandas has recently refactored the way we import the _builtin_table and is now part of the `pandas.core.common` module instead of being an attribute of the `pandas.core.base.SelectionMixin` class.

PR that was merged in the 1.3.0 : pandas-dev/pandas#40857

This suggestion should solve the issue !
  • Loading branch information
Cedric-Magnan authored Aug 26, 2021
1 parent 35552d9 commit e971d6f
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions databricks/koalas/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,12 @@ def apply(self, func, *args, **kwargs) -> Union[DataFrame, Series]:
1 52
Name: B, dtype: int64
"""
from pandas.core.base import SelectionMixin
if LooseVersion(pd.__version__) >= LooseVersion("1.3.0"):
from pandas.core.common import _builtin_table
else:
from pandas.core.base import SelectionMixin

_builtin_table = SelectionMixin._builtin_table

if not isinstance(func, Callable): # type: ignore
raise TypeError("%s object is not callable" % type(func).__name__)
Expand Down Expand Up @@ -1133,9 +1138,9 @@ def apply(self, func, *args, **kwargs) -> Union[DataFrame, Series]:

if is_series_groupby:
name = kdf.columns[-1]
pandas_apply = SelectionMixin._builtin_table.get(func, func)
pandas_apply = _builtin_table.get(func, func)
else:
f = SelectionMixin._builtin_table.get(func, func)
f = _builtin_table.get(func, func)

def pandas_apply(pdf, *a, **k):
return f(pdf.drop(groupkey_names, axis=1), *a, **k)
Expand Down Expand Up @@ -1292,7 +1297,12 @@ def filter(self, func) -> Union[DataFrame, Series]:
5 6
Name: B, dtype: int64
"""
from pandas.core.base import SelectionMixin
if LooseVersion(pd.__version__) >= LooseVersion("1.3.0"):
from pandas.core.common import _builtin_table
else:
from pandas.core.base import SelectionMixin

_builtin_table = SelectionMixin._builtin_table

if not isinstance(func, Callable): # type: ignore
raise TypeError("%s object is not callable" % type(func).__name__)
Expand Down Expand Up @@ -1324,7 +1334,7 @@ def pandas_filter(pdf):
return pd.DataFrame(pdf.groupby(groupkey_names)[pdf.columns[-1]].filter(func))

else:
f = SelectionMixin._builtin_table.get(func, func)
f = _builtin_table.get(func, func)

def wrapped_func(pdf):
return f(pdf.drop(groupkey_names, axis=1))
Expand Down

0 comments on commit e971d6f

Please sign in to comment.