wesm/pandas — Issue #237: Groupby apply mixed-type casting problem
Author: wesm
Created: 2011-10-14 21:01:49+00:00
State: open
Labels: Bug

the following groupby transform results in things getting casted to object...

```python
df = DataFrame({'d' : [1.,1.,1.,2.,2.,2.],
'c' : np.tile(['a','b','c'], 2),
'v' : np.arange(1., 7.)})
# in IPython
In [34]: df
Out[34]:
c d v
0 a 1 1
1 b 1 2
2 c 1 3
3 a 2 4
4 b 2 5
5 c 2 6
```

Now write a small transform function:

```python
def f(group):
v = group['v']
group['v2'] = (v - v.min()) / (v.max() - v.min())
return group
```

Note that this also handles NAs since the v variable is a pandas Series object.

Now group by the d column and apply f:

```python
In [36]: df.groupby('d').apply(f)
Out[36]:
c d v v2
0 a 1 1 0
1 b 1 2 0.5
2 c 1 3 1
3 a 2 4 0
4 b 2 5 0.5
5 c 2 6 1
```