xorbitsai / xorbits

Scalable Python DS & ML, in an API compatible & lightning fast way.
https://xorbits.readthedocs.io
Apache License 2.0
1.13k stars 69 forks source link

FEAT: Implements __iter__ for Groupby #593

Open qinxuye opened 1 year ago

qinxuye commented 1 year ago

Is your feature request related to a problem? Please describe

A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

__iter__ is absent for Groupby.

Describe the solution you'd like

A clear and concise description of what you want to happen.

In [1]: import xorbits.pandas as pd

In [2]: df = pd.DataFrame({'Date': ['2018-02-18', '2018-02-18', '2018-02-24', '2018-02-24'], 'Category': ['Social', 'Lunch', 'Social', 'Lunch'], 'Location': ['Terrace', 'Pox', 'Gate 320', 'Pox'], 'Expense': [98.34, 245.63, 121.89, 248], 'Balan
   ...: ce': [9971.66, 9726.03, 9604.14, 9356.04]})

In [3]: for i, df in df.groupby('Date'):
   ...:     pass
   ...: 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 for i, df in df.groupby('Date'):
      2     pass

File ~/Workspace/xorbits/python/xorbits/core/data.py:248, in DataRef.__iter__(self)
    245         except IndexError:
    246             break
--> 248 yield from gen()

File ~/Workspace/xorbits/python/xorbits/core/data.py:244, in DataRef.__iter__.<locals>.gen()
    242 while True:
    243     try:
--> 244         yield self.__getitem__(next(counter))
    245     except IndexError:
    246         break

File ~/Workspace/xorbits/python/xorbits/core/adapter.py:294, in wrap_magic_method.<locals>.wrapped(self, *args, **kwargs)
    290     raise AttributeError(
    291         f"'{self.data.data_type.name}' object has no attribute '{method_name}'"
    292     )
    293 else:
--> 294     return wrap_mars_callable(
    295         getattr(mars_entity, method_name),
    296         attach_docstring=False,
    297         is_cls_member=True,
    298     )(*args, **kwargs)

File ~/Workspace/xorbits/python/xorbits/core/adapter.py:469, in wrap_mars_callable.<locals>.wrapped(*args, **kwargs)
    467 @functools.wraps(c)
    468 def wrapped(*args, **kwargs):
--> 469     return from_mars(c(*to_mars(args), **to_mars(kwargs)))

File ~/Workspace/xorbits/python/xorbits/_mars/dataframe/groupby/getitem.py:132, in df_groupby_getitem(df_groupby, item)
    130     output_types = [OutputType.dataframe_groupby]
    131 else:
--> 132     raise NameError(f"Cannot slice groupby with {item!r}")
    134 if df_groupby.selection:
    135     raise IndexError(f"Column(s) {df_groupby.selection!r} already selected")

NameError: Cannot slice groupby with 0