Skip to content

Commit e971d6f

Browse files
[FIX] _builtin_table import in groupby apply (changed in pandas>=1.3.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 !
1 parent 35552d9 commit e971d6f

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

databricks/koalas/groupby.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,12 @@ def apply(self, func, *args, **kwargs) -> Union[DataFrame, Series]:
11051105
1 52
11061106
Name: B, dtype: int64
11071107
"""
1108-
from pandas.core.base import SelectionMixin
1108+
if LooseVersion(pd.__version__) >= LooseVersion("1.3.0"):
1109+
from pandas.core.common import _builtin_table
1110+
else:
1111+
from pandas.core.base import SelectionMixin
1112+
1113+
_builtin_table = SelectionMixin._builtin_table
11091114

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

11341139
if is_series_groupby:
11351140
name = kdf.columns[-1]
1136-
pandas_apply = SelectionMixin._builtin_table.get(func, func)
1141+
pandas_apply = _builtin_table.get(func, func)
11371142
else:
1138-
f = SelectionMixin._builtin_table.get(func, func)
1143+
f = _builtin_table.get(func, func)
11391144

11401145
def pandas_apply(pdf, *a, **k):
11411146
return f(pdf.drop(groupkey_names, axis=1), *a, **k)
@@ -1292,7 +1297,12 @@ def filter(self, func) -> Union[DataFrame, Series]:
12921297
5 6
12931298
Name: B, dtype: int64
12941299
"""
1295-
from pandas.core.base import SelectionMixin
1300+
if LooseVersion(pd.__version__) >= LooseVersion("1.3.0"):
1301+
from pandas.core.common import _builtin_table
1302+
else:
1303+
from pandas.core.base import SelectionMixin
1304+
1305+
_builtin_table = SelectionMixin._builtin_table
12961306

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

13261336
else:
1327-
f = SelectionMixin._builtin_table.get(func, func)
1337+
f = _builtin_table.get(func, func)
13281338

13291339
def wrapped_func(pdf):
13301340
return f(pdf.drop(groupkey_names, axis=1))

0 commit comments

Comments
 (0)