8000 Throw error when grouping by an unknown column (closes #81) by crew102 · Pull Request #82 · kieferk/dfply · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Throw error when grouping by an unknown column (closes #81) #82

New issue

Have a question abou 8000 t 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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
10 changes: 9 additions & 1 deletion dfply/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def _apply(self, df, *args, **kwargs):

def __call__(self, *args, **kwargs):
grouped_by = getattr(args[0], '_grouped_by', None)
if (grouped_by is None) or not all([g in args[0].columns for g in grouped_by]):
if grouped_by is None:
return self.function(*args, **kwargs)
else:
applied = self._apply(args[0], *args[1:], **kwargs)
Expand All @@ -343,3 +343,11 @@ def dfpipe(f):
symbolic_evaluation(f)
)
)


def assert_known_cols(df, cols):
bad_cols = [g for g in cols if g not in df.columns]
if bad_cols:
raise Exception(
'The following columns are unknown: ' + ', '.join(bad_cols)
)
1 change: 1 addition & 0 deletions dfply/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@pipe
@symbolic_evaluation(eval_as_label=True)
def group_by(df, *args):
assert_known_cols(df, args)
df._grouped_by = list(args)
return df

Expand Down
3 changes: 3 additions & 0 deletions test/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ def test_group_attributes():
d = diamonds >> group_by('cut')
assert hasattr(d, '_grouped_by')
assert d._grouped_by == ['cut',]

with pytest.raises(Exception, match='unknown'):
diamonds >> group_by('bad_col')
0