pandarallel icon indicating copy to clipboard operation
pandarallel copied to clipboard

groupby.parallel_apply not working int Pandas 2.0

Open alexrblohm opened this issue 3 years ago • 10 comments

General

  • Operating System: Max
  • Python version: 3.11
  • Pandas version: 2.0.0
  • Pandarallel version: 1.6.4

Acknowledgement

  • [X ] My issue is NOT present when using pandas alone (without pandarallel)
  • [ ] If I am on Windows, I read the Troubleshooting page before writing a new bug report

Bug description

When I use groupby.apply it works, but parallel_apply it does not. I'm assuming it's the new version of Pandas... AttributeError: 'DataFrameGroupBy' object has no attribute 'mutated'

Observed behavior

Write here the observed behavior

ttributeError Traceback (most recent call last) Cell In[20], line 4 1 from pandarallel import pandarallel 3 pandarallel.initialize(use_memory_fs=False) ----> 4 df.groupby('h_col').parallel_apply(simp_pred)

File /usr/local/lib/python3.11/site-packages/pandarallel/core.py:446, in parallelize_with_pipe..closure(data, user_defined_function, *user_defined_function_args, **user_defined_function_kwargs) 442 progress_bars.set_error(worker_index) 444 results = results_promise.get() --> 446 return data_type.reduce(results, reduce_extra)

File /usr/local/lib/python3.11/site-packages/pandarallel/data_types/dataframe_groupby.py:82, in DataFrameGroupBy.Apply.reduce(datas, extra) 77 mutated = cast(List[bool], mutated) 79 args = get_args(keys, values, df_groupby) 81 return df_groupby._wrap_applied_output( ---> 82 *args, not_indexed_same=df_groupby.mutated or mutated 83 )

File /usr/local/lib/python3.11/site-packages/pandas/core/groupby/groupby.py:952, in GroupBy.getattr(self, attr) 949 if attr in self.obj: 950 return self[attr] --> 952 raise AttributeError( 953 f"'{type(self).name}' object has no attribute '{attr}'" 954 )

AttributeError: 'DataFrameGroupBy' object has no attribute 'mutated'

Expected behavior

Write here the expected behavior

Minimal but working code sample to ease bug fix for pandarallel team

from pandarallel import pandarallel import pandas as pd import numpy as np

df = pd.DataFrame({ 'h_col': ['A'] * 4 + ['B'] * 4, 'ts_data': [np.nan, 2, 3, 4] * 2, 'time_col': pd.date_range("2018-01-01", periods=4, freq="w").to_list() * 2} )

def simp_pred(df): fcst = df.iloc[3:4, :] fcst.loc[:, 'fcst'] = 1 return fcst

This works

df.groupby('h_col').apply(simp_pred)

This does not

pandarallel.initialize(use_memory_fs=False) df.groupby('h_col').parallel_apply(simp_pred)

alexrblohm avatar Apr 18 '23 18:04 alexrblohm

In dataframe_groupby.py line 81 this worked for me. I couldn't push the branch and open a pr for some reason. pandas_version = get_pandas_version() if pandas_version < (2, 0): return df_groupby._wrap_applied_output( *args, not_indexed_same=df_groupby.mutated or mutated ) else: return df_groupby._wrap_applied_output( *args, not_indexed_same=mutated )

alexrblohm avatar Apr 19 '23 00:04 alexrblohm

i think i have the same issue

Python version: 3.10.8 Pandas version: 2.0.0 Pandarallel version: 1.6.4

Error message: 'DataFrameGroupBy' object has no attribute 'mutated'

Traceback:

df.parallel_apply( File "/usr/local/lib/python3.10/site-packages/pandarallel/core.py", line 446, in closure progress_bar=False, File "/usr/local/lib/python3.10/site-packages/pandarallel/data_types/dataframe_groupby.py", line 82, in reduce *args, not_indexed_same=df_groupby.mutated or mutated File "/usr/local/lib/python3.10/site-packages/pandas/core/groupby/groupby.py", line 952, in getattr raise AttributeError( AttributeError: 'DataFrameGroupBy' object has no attribute 'mutated'

Is it the same issue?

AymanElsayeed avatar Apr 20 '23 11:04 AymanElsayeed

Fixed in #235, just need to get that released and it should be solved. Thanks for reporting the bug @AymanElsayeed and the fix @alexrblohm!

t-muser avatar Apr 20 '23 13:04 t-muser

Thank you so much!

alexrblohm avatar Apr 20 '23 14:04 alexrblohm

@till-m, thank you, when this will be merged?

AymanElsayeed avatar Apr 24 '23 12:04 AymanElsayeed

@nalepae when you are planning on pushing the next release?

t-muser avatar Apr 24 '23 13:04 t-muser

I can do it today.

Do you want the ability to do it too?

nalepae avatar Apr 24 '23 15:04 nalepae

Hi @nalepae

I'm waiting for this; if you need help merging this change, I'll gladly help.

When is the next release date?

AymanElsayeed avatar May 10 '23 07:05 AymanElsayeed

Hey @AymanElsayeed, this should be up already (v1.6.5). Do you still run into the same problem? Can you please check and make sure you're using version 1.6.5?

@nalepae, I think it's fine the way it is currently.

t-muser avatar May 10 '23 08:05 t-muser

@till-m this is working, yes, in 1.6.5. Thanks

AymanElsayeed avatar May 10 '23 08:05 AymanElsayeed