I have a panel dataset that is indexed by Date and ID and looks something like this:
df = pd.DataFrame({'Date':['2005-12-31', '2006-03-31', '2006-09-30','2005-12-31', '2006-03-31', '2006-06-30', '2006-09-30'],
'ID':[1,1,1,2,2,2,2],
'Value':[14,25,34,23,67,14,46]})
I'm trying to transfer the values โโof the same ID by date and date, maybe a continuous quarter. groupby.shift is not giving me the right thing or maybe I'm missing something. Here is what I did:
df['pre_value'] = df.groupby('ID')['Value'].shift(1)
This shifts the values โโof the same identifier, but ignores the date ... note that for is ID==1absent 2006-06-30, and therefore there must really be NaN pre_valuefor its own 2006-09-30. I also consider multi-indexing or declaring a dataset as panels, but this complicates my other calculations. Is there an easy way to do this with a dataframe?
source
share