bug in groupby.apply?, goes away if I copy the data. · Issue #5545 · pandas-dev/pandas (original) (raw)
import pandas as pd
data = pd.DataFrame({'id_field' : [100, 100, 200, 300], 'category' : ['a','b','c','c'], 'value' : [1,2,3,4]})
def filt1(x):
if x.shape[0] == 1:
return x.copy()
else:
return x[x.category == 'c']
def filt2(x):
if x.shape[0] == 1:
return x
else:
return x[x.category == 'c']
#works fine
print data.groupby('id_field').apply(filt1)
#throws error
print data.groupby('id_field').apply(filt2)