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 groupby and docs #373

Merged
merged 6 commits into from
Dec 22, 2023
Merged

add groupby and docs #373

merged 6 commits into from
Dec 22, 2023

Conversation

pdeffebach
Copy link
Collaborator

Adding a @groupby convenience macro has been suggested before (though I can't find the exact issues). Currently, you have to write

@chain df begin
    groupby([:a, :b])
    @transform ...
end

The extra [ and the lack of @ is frustrating and gets you out of the "flow" of a chained operation.

This PR allows

@chain df begin
  @groupby :a :b 
  @transfrom ...
end

which is cleaner

This macro also provides a few convenience syntaxes

  1. Allows for mixing String and Symbol column selectors
  2. Allows for "block" format
  3. Automatically concatenates to allow for scalar and vector selection

Unlike DataFrameMacros.jl's @groupby is does not allow for new column creation. This is intentional. I don't want @groupby to create any copies or have any ambiguity about when it creates a copy, which DataFrameMacros.jl's @groupby does (see here)`.

With my implementation, users can mindlessly write @groupby without worrying about performance at all.

Copy link
Member

@bkamins bkamins left a comment

Choose a reason for hiding this comment

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

Looks good, but I would recommend to make it more precise how args... are transformed to a single argument to groupby as it was not fully clear to me. What if someone e.g. writes
@groupby(df, Cols(r"a"), Not("b")).

@pdeffebach
Copy link
Collaborator Author

pdeffebach commented Dec 22, 2023

@bkamins Ready for a review.

I opted against adding any escaping rules for @groupby df x. @groupby df x y is now exactly the same as groupby(df, Cols(x, y)). No $ or anything else needed.

  1. This corresponds with @by, which is our other maco which allows grouping.
  2. This allows for @groupby df [:a, :b] easily. Users don't really need to do [:a, :b] in @groupby when they could just do @groupby df :a :b, but they do need to do that in @by, which doesn't allow grouping in multiple expressions.
  3. Why isn't [:a, :b] special-cased in Allow for Between, Not, All, Cols in @select #372, which allows Cols, All() etc? Maybe it should have been, but 1. [:a, :b] might actually signal an actual transformation somewhere and doesn't unambiguously mean a column selection. [:a, :b] and [i for i in ...] actually lower to different expressions and that seems confusing.
  4. It's simpler and easier to explain to users.

@pdeffebach pdeffebach merged commit cdfb733 into master Dec 22, 2023
13 of 19 checks passed
@pdeffebach pdeffebach deleted the groupby_maco branch December 22, 2023 19:50
@pdeffebach
Copy link
Collaborator Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants