データ分析実践
上級問題チェック
問題 26 /40
pandasのinterpolateメソッドの説明として、誤っているものはどれか。
選択 1
interpolate(method='linear')
▶︎
前後の値と等間隔になるように欠損値を補間する。
選択 2
interpolate(method='index')
▶︎
インデックスの間隔を考慮して欠損値を補間する。
選択 3
interpolate(method='values')
▶︎
前後の値を考慮して欠損値を補間する。
選択 4
interpolate(method='time')
▶︎
DatetimeIndexの間隔を考慮して欠損値を補間する。
解説
選択肢3が正解です。
【選択肢1】
interpolate(method='linear')
▶︎
前後の値と等間隔になるように補間する。
正しい説明です。
method='linear'は線形補間です。インデックスの間隔は考慮せず、欠損値の前後の値をベースにして、等間隔になるよう補間します。
ser = pd.Series(
[100,np.NaN,900],
index=[1,9,10])
▶︎
1 100.0
9 NaN
10 900.0
ser.interpolate(method='linear')
▶︎
1 100.0
9 500.0
10 900.0
100と900の間が等間隔になるように、500が補間されています。
interpolateメソッドはデフォルトがmethod='linear'のため、引数をつけなかった場合は線形補間になります。
【選択肢2】
interpolate(method='index')
▶︎
インデックスの間隔を考慮して欠損値を補間する。
正しい説明です。
method='index'は線形補間と違い、インデックスの間隔を考慮して補間します。
ser
▶︎
1 100.0
9 NaN
10 900.0
ser.interpolate(method='index')
▶︎
1 100.000000
9 811.111111
10 900.000000
インデックス9は10に近いため、データも900に近い811.111111が補間されています。
なお、method='index'はインデックスが数値の場合だけ使えます。
【選択肢3】
interpolate(method='values')
▶︎
前後の値を考慮して欠損値を補間する。
誤った説明です。
選択肢2のmethod='index'と同じで、インデックスの間隔を考慮して補間します。
【選択肢4】
interpolate(method='time')
▶︎
DatetimeIndexの間隔を考慮して補間する。
正しい説明です。
method='time'は、インデックスの日時の間隔を考慮して補間します。
ser = pd.Series(
[100,np.NaN,900],
index=pd.DatetimeIndex(['00:00','00:01','00:20']))
▶︎
2025-06-15 00:00:00 100.0
2025-06-15 00:01:00 NaN
2025-06-15 00:20:00 900.0
ser.interpolate(method='time')
▶︎
2025-06-15 00:00:00 100.0
2025-06-15 00:01:00 140.0
2025-06-15 00:20:00 900.0
インデックスの01分は00分に近いため、データも100に近い140が補間されています。
なお、method='time'はインデックスがDatetimeIndexの場合だけ使えます。
(公式書籍 p.200-201)