pydata/pandas — pull request #5518 by hayd (2013-11-15 00:30:38+00:00)
Title: PERF faster head, tail and size groupby methods

This is some low hanging fruit, significantly faster than master.

Unfortunately it'd going to fail Travis due to an as_index, which I'm a little confused about (not sure how this was working in master) as I'm unsure how to grab out the final index with the index - surely there's something obvious ??

To give some numbers, before the change is below the new:
```
In [1]: df = pd.DataFrame(np.random.randint(0, 100, 1000), columns=['A'], index=[0] * 1000); g = df.groupby('A')

In [2]: %timeit g.head(2)
1000 loops, best of 3: 429 µs per loop
# 100 loops, best of 3: 9.67 ms per loop

In [3]: %timeit g.tail(2)
1000 loops, best of 3: 398 µs per loop
# 100 loops, best of 3: 9.68 ms per loop

In [4]: %timeit g.size()
10000 loops, best of 3: 119 µs per loop
# 1000 loops, best of 3: 649 µs per loop

In [11]: df = pd.DataFrame(np.random.randint(0, 10, 1000), columns=['A'], index=[0] * 1000); g = df.groupby('A')

In [12]: %timeit g.head(2)
10000 loops, best of 3: 189 µs per loop
# 100 loops, best of 3: 2.1 ms per loop

In [13]: %timeit g.tail(2)
10000 loops, best of 3: 160 µs per loop
# 100 loops, best of 3: 2.11 ms per loop

In [14]: %timeit g.size()
10000 loops, best of 3: 41.9 µs per loop
# 1000 loops, best of 3: 598 µs per loop
```

Reason it's failing, I can't deal with this case (I'm sure it's super obvious), this is what happens in master:
```
In [1]: df = pd.DataFrame([[1, 2], [1, 4], [3, 6]], columns=['A', 'B']); g = df.groupby('A', as_index=True)

In [2]: g.head(1)
Out[2]:
A B
A
1 0 1 2
3 2 3 6

In [3]: df = pd.DataFrame([[1, 2], [1, 4], [3, 6]], columns=['A', 'B']); g = df.groupby('A', as_index=False)

In [4]: g.head(1)
Out[4]:
A B
0 1 2
2 3 6
```
Any ideas?