Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas - identify rows closest to null/sentinel value

Working with a time series dataframe of below format with the first column as index:

08/11/2021, 100
08/12/2021, null
08/13/2021, null
08/14/2021, 160

I want to perform an interpolation to return

08/11/2021, 100
08/12/2021, 120
08/13/2021, 140
08/14/2021, 160

What's an effective way to get the row indices nearest to the first and last null values in my df without pulling out the iterrows() sledgehammer? (Assume there can be multiple spans of nulls throughout the series and I will repeat this operation every time the function encounters a null)

like image 613
Thomas Murphy Avatar asked Jan 20 '26 17:01

Thomas Murphy


1 Answers

No need to do anything manually, just use interpolate:

df.interpolate()
like image 60
mozway Avatar answered Jan 23 '26 05:01

mozway