为什么np.percentile返回高百分位数的NaN?

Tho*_*son 4 python numpy pandas

这段代码:

print len(my_series)
print np.percentile(my_series, 98)
print np.percentile(my_series, 99)
Run Code Online (Sandbox Code Playgroud)

给出:

14221  # This is the series length
1644.2  # 98th percentile
nan  # 99th percentile?
Run Code Online (Sandbox Code Playgroud)

为什么98可以正常工作而99可以正常工作nan

Nie*_*ens 5

np.percentile将nan视为非常高/无限的数字。因此,高百分位数将处于您将得到nan的范围内。在您的情况下,数据的1-2%将是nan(第98个百分位数将为您返回一个数字(实际上不是所有有效值的第98个百分位数),而第99个百分数将为您返回nan)。

要计算不包含nan的百分位数,可以使用np.nanpercentile()

所以:

print np.nanpercentile(my_series, 98)
print np.nanpercentile(my_series, 99)
Run Code Online (Sandbox Code Playgroud)