Here is my code

```
from pandas import DataFrame, Series
import pandas as pd
import numpy as np
income = DataFrame({'name': ['Adam', 'Bill', 'Chris', 'Dave', 'Edison', 'Frank'],
                    'age': [22, 24, 31, 45, 51, 55],
                    'income': [1000, 2500, 1200, 1500, 1300, 1600],
                    })
ageBin = pd.cut(income.age, [20, 30, 40, 50, 60])
grouped = income.groupby([ageBin])
highestIncome = income.ix[grouped.income.idxmax()]

```

I have a DataFrame that contains names, ages and income as follows:

```
index   age income  name
0   22  1000    Adam
1   24  2500    Bill
2   31  1200    Chris
3   45  1500    Dave
4   51  1300    Edison
5   55  1600    Frank

```

I would like to group the data by the age bins and collect the records with highest income. The code above works and the `highestIncome` is:

```
index   age income  name
1   24  2500    Bill
2   31  1200    Chris
3   45  1500    Dave
5   55  1600    Frank

```

However, if I delete the record of Chris and thus there is no record within the age range of *(30, 40]*, I get a `ValueError` at `grouped.income.idxmax()`. I think this is because of the `NaN` in grouped, but I cannot find a way to solve the problem. Any input is appreciated.

**Update:** Thanks a lot for the answers. I do believe this is a bug on idxmax() for groupby objects. I would like to go with the `agg(lambda x: x.idxmax())` approach as I did a test of the speed of using `sort()` vs `agg(lambda x: x.idxmax()` on a 10 million synthetic data set. Here is the code and the output:

```
from pandas import DataFrame, Series
import pandas as pd
import numpy as np
import time

testData = DataFrame({'key': np.random.randn(10000000),
                      'value': np.random.randn(10000000)})
keyBin = pd.cut(testData.key, 1000)

start = time.time()
grouped1 = testData.sort('value', ascending=False).groupby([keyBin])
highestValues1 = testData.ix[grouped1.head(1).index]
end = time.time()
print end - start

start = time.time()
grouped2 = testData.groupby([keyBin])
highestValues2 = testData.ix[grouped2.value.agg(lambda x: x.idxmax())].dropna(how='all')
end = time.time()
print end - start
#validation
(highestValues1.sort() == highestValues2.sort()).all()

```

Output:

```
5.30953717232
1.0279238224

Out[47]:

key      True
value    True
dtype: bool

```