Interpolate only if single NaN

前端 未结 2 1713
清歌不尽
清歌不尽 2021-02-10 14:33

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:31

    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.

提交回复
热议问题