push apply into groupby

There may be further optimizations, but this catches some huge ones.

Fix #311, fix #298