Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion pandarallel/data_types/dataframe_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,24 @@ def get_chunks(
chunks = chunk(dataframe_groupby.ngroups, nb_workers)
iterator = iter(dataframe_groupby)

# Since pandas 3.0, `DataFrameGroupBy.apply` operates on
# `_obj_with_exclusions`, i.e. the grouping columns are no longer passed to
# the applied function. Iterating a `DataFrameGroupBy` still yields them
# though, so restrict each group to the columns `apply` would have used.
# On pandas < 3.0 the grouping columns are still passed, so keep them.
columns = (
dataframe_groupby._obj_with_exclusions.columns
if get_pandas_version() >= (3, 0)
else None
)

for chunk_ in chunks:
yield [next(iterator) for _ in range(chunk_.stop - chunk_.start)]
groups = [next(iterator) for _ in range(chunk_.stop - chunk_.start)]

if columns is not None:
groups = [(key, df[columns]) for key, df in groups]

yield groups

@staticmethod
def work(
Expand Down
8 changes: 6 additions & 2 deletions tests/test_pandarallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,16 @@ def func(x):

@pytest.fixture()
def func_dataframe_groupby_apply():
# Uses column `c`, which is never a grouping column in the tests below. Since pandas
# 3.0 the grouping columns are not passed to the applied function, so a function
# reading `df.b` would fail on the `groupby(["a", "b"])` case before `parallel_apply`
# is even reached.
def func(df):
dum = 0
for item in df.b:
for item in df.c:
dum += math.log10(math.sqrt(math.exp(item**2)))

return dum / len(df.b)
return dum / len(df.c)

return func

Expand Down
Loading