pydata/pandas — pull request #3599 by jreback (2013-05-14 11:54:01+00:00)
Title: BUG: Add reduce_if_possible keyword to ``groupby`` to allow reduction in returned type

This will allow a reduction in the returned type from DataFrame -> Series
if the groups are unique.

This is a fix for a regression from 0.10.1.

Allows functionaility in #2893, by specifying ``reduce_if_possible=True``
in groupby call. #3596 functionaility is back as the default

This returns a Series because we are passing ``reduce_at_possible=True``
```
In [9]: df2 = DataFrame([{"val1": 1, "val2" : 20},
{"val1":1, "val2": 19},{"val1":1, "val2": 27}, {"val1":1, "val2": 12}])

In [10]: df2
Out[10]:
val1 val2
0 1 20
1 1 19
2 1 27
3 1 12

In [11]: def func(dataf):
....: return dataf["val2"] - dataf["val2"].mean()
....:

In [12]: df2.groupby("val1", reduce_if_possible=True).apply(func)
Out[12]:
0 0.5
1 -0.5
2 7.5
3 -7.5
Name: 1, dtype: float64
```

Traditionally returns a DataFrame (even though have unique groups)

```
In [13]: df = DataFrame([[1,1],[1,1]],columns=['X','Y'])

In [14]: df
Out[14]:
X Y
0 1 1
1 1 1

In [15]: df.groupby('X').count()
Out[15]:
X Y
X
1 2 2
```