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

Allow empty keys argument in by() #1837

Closed
alecloudenback opened this issue May 31, 2019 · 5 comments
Closed

Allow empty keys argument in by() #1837

alecloudenback opened this issue May 31, 2019 · 5 comments

Comments

@alecloudenback
Copy link
Contributor

When exploring data or creating a function to abstract the process of generating a fixed set of results for different combinations of variables, it would be nice to be able to pass an empty array which would simply "group-by" all the rows in the dataset, e.g.

function summarize(varies_by)

    by(iris,varies_by) do df
          (m = mean(df.PetalLength), s² = var(df.PetalLength))
    end
end

summarize([])

would return

m
1 3.75867 3.11318

Currently it errors:

BoundsError: attempt to access ()
  at index [1]
@nalimilan
Copy link
Member

Currently GroupedDataFrame isn't prepared to handle this, but I guess that would be doable (putting all rows in the same group). Is this allowed by other implementations (like dplyr and Pandas)?

@alecloudenback
Copy link
Contributor Author

alecloudenback commented Jun 1, 2019

In R:

library(dplyr)
starwars %>%
  group_by(species) %>%
  summarise(
    n = n(),
    mass = mean(mass, na.rm = TRUE)
  )

returns

# A tibble: 38 x 3
   species       n  mass
   <chr>     <int> <dbl>
 1 <NA>          5  48  
 2 Aleena        1  15  
 3 Besalisk      1 102  
 4 Cerean        1  82  
 5 Chagrian      1 NaN  
 6 Clawdite      1  55  
 7 Droid         5  69.8
 8 Dug           1  40  
 9 Ewok          1  20  
10 Geonosian     1  80  
# ... with 28 more rows

And leaving the group_by empty just aggregates across all:

starwars %>%
  group_by() %>%
  summarise(
    n = n(),
    mass = mean(mass, na.rm = TRUE)
  )

returns:

# A tibble: 1 x 2
      n  mass
  <int> <dbl>
1    87  97.3

@alecloudenback
Copy link
Contributor Author

While R does (above), Pandas does not:

> grouped = df.groupby()
TypeError: You have to supply one of 'by' and 'level'

@nalimilan
Copy link
Member

OK, then I guess we could support this too. Though that's not high priority for us, but feel free to make a pull request.

@bkamins
Copy link
Member

bkamins commented Dec 1, 2019

fixed

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

No branches or pull requests

3 participants