自熊猫0.25.1起,存在可为空的整数类型。但是,我发现缺少某些功能。下面的示例说明如何将可空整数用于基本的逐点算术,但不能用于线性插值。插值函数似乎将pd.Int64Dtype()
视为通用object
dtype。
最小工作示例:
import numpy as np
import pandas as pd
s = pd.Series([0, 1, np.nan, 3])
u = s.astype(pd.Int64Dtype())
print(s)
print(s.interpolate())
print(u)
print(s+100)
print(u+100)
print(s*u)
try:
print(u.interpolate())
except Exception as e:
print(e)
print(u.interpolate(method='ffill'))
输出:
0 0.0
1 1.0
2 NaN
3 3.0
dtype: float64
0 0.0
1 1.0
2 2.0
3 3.0
dtype: float64
0 0
1 1
2 NaN
3 3
dtype: Int64
0 100.0
1 101.0
2 NaN
3 103.0
dtype: float64
0 100
1 101
2 NaN
3 103
dtype: Int64
0 0.0
1 1.0
2 NaN
3 9.0
dtype: float64
Invalid fill method. Expecting pad (ffill) or backfill (bfill). Got linear
0 0
1 1
2 1
3 3
dtype: Int64
我期望得到的答案: