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

Feature: Iterable Groupby Class #595

Open
andria-dev opened this issue Aug 27, 2023 · 0 comments
Open

Feature: Iterable Groupby Class #595

andria-dev opened this issue Aug 27, 2023 · 0 comments
Labels
enhancement New feature or request Some day maybe We might consider adding/fixing this in the future

Comments

@andria-dev
Copy link

What is the feature?

I'd love to be able to iterate over the groups in a Groupby. The only way that I know how to iterate over a Groupby currently is to use GroupBy#apply() — which is not documented, does not give the grouped value, and does not offer the same loop control as an iterable. I would prefer a more natural and readable approach for iteration (preferably a `for ... of`` loop).

Solution

I'd like the Groupby class to implement the iterable protocol with Symbol.iterator so that someone can use a for ... of loop (or even the spread syntax ...) to iterate over each group's value that it was grouped by and the data in that group as a DataFrame. This could be implemented similarly to how For example:

let dataframe = new dfd.DataFrame([
  {category: 'a', valueA: 123, valueB: 456},
  {category: 'a', valueA: 22, valueB: 56},
  {category: 'b', valueA: 11, valueB: 314},
  {category: 'b', valueA: 155, valueB: 2222},
])
for (const [category, dataframe] of dataframe.groupby(['category'])) {
  console.log(category) // a
  dataframe.print()
  /**
    ╔═══╤══════════╤════════╤════════╗
    ║   │ category │ valueA │ valueB ║
    ╟───┼──────────┼────────┼────────╢
    ║ 0 │ a        │ 123    │ 456    ║
    ╟───┼──────────┼────────┼────────╢
    ║ 1 │ a        │ 22     │ 56     ║
    ╚═══╧══════════╧════════╧════════╝
   */
}

The documentation would also need to be updated to inform people that Groupby is iterable, preferably with an example of such.

Alternatives

You could also provide only each group's DataFrame (i.e. no value that it was grouped by), but that would be a bit annoying since I can't think of any situation where you'd want to group the data and then ignore the value that was used for grouping.

A complete alternative could be implementing a forEach method, but it would be quite odd to only implement the forEach method without implementing the iterable protocol given that implementing either of them is almost the same process while the iterable protocol gives greater control.

@steveoni steveoni added enhancement New feature or request good first issue Good for newcomers labels Sep 30, 2023
@risenW risenW added Some day maybe We might consider adding/fixing this in the future and removed good first issue Good for newcomers labels Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Some day maybe We might consider adding/fixing this in the future
Projects
None yet
Development

No branches or pull requests

3 participants