Reputation: 1741
Why doesn't this code work in version pandas .13? is it a bug or do i need to do things differently?
previous=0
for i, r in df.iterrows():
if i in df.index[df.groupby(['case']).apply(lambda x: x['change'].idxmin())]:
print 'anything'
i can't find any documentation that shows why.
for testing
import pandas as pd
import datetime as DT
d = {'case' : pd.Series([1,1,1,1,2]),
'open' : pd.Series([DT.datetime(2014, 3, 2), DT.datetime(2014, 3, 2),DT.datetime(2014, 3, 2),DT.datetime(2014, 3, 2),DT.datetime(2014, 3, 2)]),
'change' : pd.Series([DT.datetime(2014, 3, 8), DT.datetime(2014, 4, 8),DT.datetime(2014, 5, 8),DT.datetime(2014, 6, 8),DT.datetime(2014, 6, 8)]),
'StartEvent' : pd.Series(['Homeless','Homeless','Homeless','Homeless','Jail']),
'ChangeEvent' : pd.Series(['Homeless','irrelivant','Homeless','Jail','Jail']),
'close' : pd.Series([DT.datetime(2015, 3, 2), DT.datetime(2015, 3, 2),DT.datetime(2015, 3, 2),DT.datetime(2015, 3, 2),DT.datetime(2015, 3, 2)])}
df=pd.DataFrame(d)
Upvotes: 0
Views: 63
Reputation: 129038
Very odd to do it this way, what would you expect the result to be?
If you really really want to do this, use the value. A Series is not a valid indexer to df.index
(only an array is). This 'worked' in 0.12, but happened to rely on the fact that a Series was an ndarray sub-class.
df.index[df.groupby(['case']).apply(lambda x: x['change'].idxmin()).values]
Int64Index([0, 4], dtype='int64')
Upvotes: 1