I have a data frame with time-series features. I want to impute missing values with row-wise linear imputation.
As a reproducible example:
import pandas as pd
import numpy as np
df = pd.DataFrame({'id': range(2),
'F1_Date_1': [1,2],
'F1_Date_2': [np.nan,4],
'F1_Date_3': [3, 6],
'F1_Date_4': [4,8],
'F2_Date_1': [2,11],
'F2_Date_2': [6, np.nan],
'F2_Date_3': [10, np.nan],
'F2_Date_4': [14, 17]})
df
id F1_Date_1 F1_Date_2 ... F2_Date_2 F2_Date_3 F2_Date_4
0 0 1 NaN ... 6.0 10.0 14
1 1 2 4.0 ... NaN NaN 17
For F1, I want to linearly impute (interpolate) F1_Date_2 using F1_Date_1 and F1_Date_3. For F2, I want to impute F2_Date_2 and F2_Date_3 using F2_Date_1 and F2_Date_4
The desired output is
final_df = pd.DataFrame({'id': range(2),
'F1_Date_1': [1,2],
'F1_Date_2': [2,4],
'F1_Date_3': [3, 6],
'F1_Date_4': [4,8],
'F2_Date_1': [2,11],
'F2_Date_2': [6, 13],
'F2_Date_3': [10, 15],
'F2_Date_4': [14, 17]})
id F1_Date_1 F1_Date_2 ... F2_Date_2 F2_Date_3 F2_Date_4
0 0 1 2 ... 6 10 14
1 1 2 4 ... 13 15 17
How can I do that on a very large dataset (potentially 10 million rows, 8 dates, and 15 features) efficiently in Python?