Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of group_by+mutate #242

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions tidypolars/tibble.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,26 @@ def write_parquet(self,
"""Write a data frame to a parquet"""
return super().write_parquet(file, compression = compression, use_pyarrow = use_pyarrow, **kwargs)

def group_by(self, group, *args, **kwargs):
dafxy marked this conversation as resolved.
Show resolved Hide resolved
res = TibbleGroupBy(self, group, maintain_order=True)
return res

class TibbleGroupBy(pl.dataframe.group_by.GroupBy):

def __init__(self, df, by, *args, **kwargs):
assert isinstance(by, str) or isinstance(by, list), "Use list or string to group by."
super().__init__(df, by, *args, **kwargs)
self.df = df
self.by = by if isinstance(by, list) else list(by)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a little tweek here:
self.by = by if isinstance(by, list) else [by]


@property
def _constructor(self):
return TibbleGroupBy

def mutate(self, *args, **kwargs):
out = self.map_groups(lambda x: from_polars(x).mutate(*args, **kwargs))
return out

def desc(x):
"""Mark a column to order in descending"""
x = copy.copy(x)
Expand Down