Skip to content

Commit 517c997

Browse files
author
sjoshistrats
authored
Merge pull request #3 from sjoshistrats/feature/docs
Add docs to README
2 parents f542d72 + 91a4f05 commit 517c997

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,55 @@
11
[![<sjoshistrats>](https://circleci.com/gh/sjoshistrats/fastgrouper.svg?style=svg)](https://app.circleci.com/pipelines/github/sjoshistrats/fastgrouper?branch=master)
22

33
# 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

Comments
 (0)