次の Python コードの出力はどれでしょうか?
Python コード:
import pandas as pd
import numpy as np
ts = pd.Series(
[1, np.nan, np.nan, 4, 5],
index=pd.date_range(
'2023-01-01',
periods=5)
)
print(ts.interpolate())
import pandas as pd
import numpy as np
ts = pd.Series(
[1, np.nan, np.nan, 4, 5],
index=pd.date_range(
'2023-01-01',
periods=5)
)
print(ts.interpolate())
import pandas as pd import numpy as np ts = pd.Series( [1, np.nan, np.nan, 4, 5], index=pd.date_range( '2023-01-01', periods=5) ) print(ts.interpolate())
回答の選択肢:
A. 1, 2, 3, 4, 5
B. 1, NaN, NaN, 4, 5
C. 1, 1.5, 2, 4, 5
D. 1, NaN, 4, 4.5, 5
出力例:
2023-01-01 1.0
2023-01-02 2.0
2023-01-03 3.0
2023-01-04 4.0
2023-01-05 5.0
Freq: D, dtype: float64
2023-01-01 1.0
2023-01-02 2.0
2023-01-03 3.0
2023-01-04 4.0
2023-01-05 5.0
Freq: D, dtype: float64
2023-01-01 1.0 2023-01-02 2.0 2023-01-03 3.0 2023-01-04 4.0 2023-01-05 5.0 Freq: D, dtype: float64
正解:
A
解説:
interpolate()
関数は、欠損値を補完するために直線的(線形)な方法を使用します。具体的には、欠損値の前後の値を使用して直線を生成し、その直線上の該当する点を欠損値の補完値とします。
ちなみに、ts
に格納されているデータは次のようになっています。
2023-01-01 1.0
2023-01-02 NaN
2023-01-03 NaN
2023-01-04 4.0
2023-01-05 5.0
Freq: D, dtype: float64
2023-01-01 1.0
2023-01-02 NaN
2023-01-03 NaN
2023-01-04 4.0
2023-01-05 5.0
Freq: D, dtype: float64
2023-01-01 1.0 2023-01-02 NaN 2023-01-03 NaN 2023-01-04 4.0 2023-01-05 5.0 Freq: D, dtype: float64