仅在单个NaN时进行插值 [英] Interpolate only if single NaN

查看:86
本文介绍了仅在单个NaN时进行插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

熊猫中是否有办法仅对单个缺失的数据点进行插值?也就是说,如果连续有2个以上的NaN,我想不理会它们.

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.

因此,例如:

s = pd.Series([1, None, 2, 3, None, None, 4.5])
d.interpolate(limit=1)

给我:

[ 1.0, 1.5, 2.0, 3.0, 3.5, NaN, 4.5 ]

但我想得到

[ 1.0, 1.5, 2.0, 3.0, NaN, NaN, 4.5 ]

如果有帮助,我会列出所有缺少单个值的索引列表.

If it helps, I have a list of the indexes where there are only single missing values.

推荐答案

我的看法是,这将是包含在interpolate中的强大功能.
就是说,这归结为掩盖存在多个np.nan的地方.我将在方便的函数中使用一些numpy逻辑将其包装起来.

My opinion is that this would be a great capability to include in interpolate.
That said, this boils down to masking the places where more than one np.nan exist. I'll wrap that up with some numpy logic in a handy function.

def cnan(s):
    v = s.values
    k = v.size
    n = np.append(np.isnan(v), False)
    m = np.empty(k, np.bool8)
    m.fill(True)
    i = np.where(n[:-1] & n[1:])[0] + np.arange(2)
    m[i[i < k]] = False
    return m

s.interpolate().where(cnan(s))

0    1.0
1    1.5
2    2.0
3    3.0
4    NaN
5    NaN
6    4.5
dtype: float64


对于那些对使用高级numpy技术的通用解决方案感兴趣的人


For those interested in a general solution using advanced numpy techniques

import pandas as pd
import numpy as np
from numpy.lib.stride_tricks import as_strided as strided

def mask_knans(a, x):
    a = np.asarray(a)
    k = a.size
    n = np.append(np.isnan(a), [False] * (x - 1))
    m = np.empty(k, np.bool8)
    m.fill(True)

    s = n.strides[0]
    i = np.where(strided(n, (k + 1 - x, x), (s, s)).all(1))[0][:, None]
    i = i + np.arange(x)
    i = pd.unique(i[i < k])

    m[i] = False

    return m

演示

demo

a = np.array([1, np.nan, np.nan, np.nan, 3, np.nan, 4, 5, np.nan, np.nan, 6, 7])

print(mask_knans(a, 3))

[ True False False False  True  True  True  True  True  True  True  True]

这篇关于仅在单个NaN时进行插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆