|
1 | 1 | [](https://app.circleci.com/pipelines/github/sjoshistrats/fastgrouper?branch=master) |
2 | 2 |
|
3 | 3 | # fastgrouper |
4 | | -Efficient tools for groupby-apply operations, in python. |
| 4 | +Allows for fast groupby-apply operations, in python. |
| 5 | + |
| 6 | +# Usage |
| 7 | + |
| 8 | +Use the `arr` interface, for numpy array focused applications. |
| 9 | + |
| 10 | +```python |
| 11 | +import numpy as np |
| 12 | +import fastgrouper.li |
| 13 | + |
| 14 | +def baz(x, y): |
| 15 | + return np.mean(x + y) - 3 |
| 16 | + |
| 17 | +# Sample arrays, to slice |
| 18 | +xvals = np.array([1, 2, 10]) |
| 19 | +yvals = np.array([4, 5, 6]) |
| 20 | + |
| 21 | +# Group IDS |
| 22 | +gids = np.array([1, -3, 1]) |
| 23 | + |
| 24 | +# Perform groupby-apply; note that keyword args are supported as well. |
| 25 | +grpd = fastgrouper.arr.Grouped(gids) |
| 26 | +result = grpd.apply(baz, xvals, y=yvals) # np.array([7.5, 4]) |
| 27 | + |
| 28 | +# Perform groupby-apply, and then expand results back to align with the gids provided. |
| 29 | +result = grpd.apply_expand(baz, xvals, yvals) # np.array([7.5, 4. , 7.5]) |
| 30 | + |
| 31 | +# Perform groupby-apply, and then expand results back to align with the gids provided. |
| 32 | +result = grpd.apply_expand(baz, xvals, yvals) # np.array([7.5, 4. , 7.5]) |
| 33 | +``` |
| 34 | + |
| 35 | +The `li` interface returns the results over the groups as a list (instead of an array); this may be useful for functions that return different-sized results. Note that in all interfaces (e.g. both `arr` and `li`), the order in which the group elements appear is preserved when the group slices are passed to the function being applied. |
| 36 | + |
| 37 | + |
| 38 | +```python |
| 39 | +import numpy as np |
| 40 | +import fastgrouper.li |
| 41 | + |
| 42 | +def bop(x): |
| 43 | + return list(x) |
| 44 | + |
| 45 | +# Sample arrays, to slice |
| 46 | +xvals = np.array([2, 3, 4]) |
| 47 | + |
| 48 | +# Group IDS |
| 49 | +gids = np.array([10, -20, 10]) |
| 50 | + |
| 51 | +grpd = fastgrouper.li.Grouped(gids) |
| 52 | +grpd.apply(bop, xvals) # returns [[2, 4], [3]] |
| 53 | +``` |
| 54 | + |
| 55 | +For additional examples, checkout the [tests](https://github.com/sjoshistrats/fastgrouper/tree/master/python/fastgrouper/test). |
0 commit comments