Interpolate only if single NaN

前端 未结 2 1382
面向向阳花
面向向阳花 2021-02-10 14:26

Is there a way in pandas to interpolate only single missing data points? That is, if there is 2+ consecutive NaN\'s, I\'d like to leave them alone.

so, as an example:

2条回答
  •  一整个雨季
    2021-02-10 15:12

    s[(s.shift(-1).notnull()) & (s.shift(1).notnull())] = (s.shift(-1) + s.shift(1))/2
    

    Actually,

    s[s.isnull()] = (s.shift(-1) + s.shift(1))/2
    

    works as well, if you are doing simple interpolation.

提交回复
热议问题