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

Add ability to pass positional arguments to summarize() #70

Merged
merged 4 commits into from
Jan 18, 2019
Merged
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
12 changes: 11 additions & 1 deletion dfply/summarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@


@dfpipe
def summarize(df, **kwargs):
def summarize(df, *args, **kwargs):
for e, v in enumerate(args):
column_name = "unnamed_arg_{}".format(e)
if column_name not in kwargs:
kwargs[column_name] = v
else:
raise KeyError(
"Positional argument {} was assigned "
"name '{}', which was also supplied as "
"a keyword argument.".format(e, column_name)
)
return pd.DataFrame({k: [v] for k, v in kwargs.items()})


Expand Down
22 changes: 22 additions & 0 deletions test/test_summarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ def test_summarize():
summarize(price_mean=X.price.mean(), price_std=X.price.std()))


def test_summarize_with_positional_args():
with_args = diamonds >> summarize(
X.price.mean(),
price_std=X.price.std()
) >> rename(
price_mean=X.unnamed_arg_0
)

with_kwargs = diamonds >> summarize(
price_std=X.price.std(),
price_mean=X.price.mean()
)

# Use `sort_index()` to account for
# Python versions < 3.6 which do not
# reliably conserve dictionary insertion order.
pd.testing.assert_frame_equal(
with_kwargs.sort_index(axis=1),
with_args.sort_index(axis=1)
)


def test_summarize_each():
to_match = pd.DataFrame({
'price_mean':[np.mean(diamonds.price)],
Expand Down