pandas.interpolate, which is mainly a wrapper for scipy
's interpolation functions, has many keywords that allow you to adapt your interpolation. You could use spline
:
d = {'col1': [np.nan, np.nan, 1, 5, 9, np.nan],
'col2': [1, 1, 2, 5, 8, np.nan],
'col3': [2, 2, 3, 4, 5, np.nan],
'col4': [np.nan, 3, np.nan, 5, 6, np.nan]}
df = pd.DataFrame(data=d)
df = df.interpolate(method = "spline", order = 1, limit_direction = "both")
print(df)
Output:
col1 col2 col3 col4
0 -7.0 1.0 2.0 2.0
1 -3.0 1.0 2.0 3.0
2 1.0 2.0 3.0 4.0
3 5.0 5.0 4.0 5.0
4 9.0 8.0 5.0 6.0
5 13.0 8.8 5.6 7.0
Edit:
There are probably more elegant solutions in pandas but here is one way to address the problem:
d = {'col1 Mar': [np.nan, np.nan, 1],
'col2 Jun': [1, 1, 2],
'col3 Sep': [2, 2, 3],
'col4 Dec': [np.nan, 3, np.nan]}
df = pd.DataFrame(data=d)
print(df)
#store temporarily the column index
col_index = df.columns
#transcribe month into a number that reflects the time distance
df.columns = [3, 6, 9, 12]
#interpolate over rows
df = df.interpolate(method = "spline", order = 1, limit_direction = "both", axis = 1, downcast = "infer")
#assign back the original index
df.columns = col_index
print(df)
Output:
col1 Mar col2 Jun col3 Sep col4 Dec
0 NaN 1 2 NaN
1 NaN 1 2 3.0
2 1.0 2 3 NaN
col1 Mar col2 Jun col3 Sep col4 Dec
0 0 1 2 3
1 0 1 2 3
2 1 2 3 4
If you provide the column index as a datetime object, you probably can use the column index directly, but I am not sure about it.
Edit 2:
As expected, you can also use datetime objects as column names to interpolate:
CSV file
Mar 2014, Jun 2014, Sep 2014, Mar 2015
nan, 1, 2, nan
nan, 1, 2, 4
1, 2, 3, nan
Code:
#read CSV file
df = pd.read_csv("test.txt", sep = r',\s*')
#convert column names to datetime objects
df.columns = pd.to_datetime(df.columns)
#interpolate over rows
df = df.interpolate(method = "spline", order = 1, limit_direction = "both", axis = 1, downcast = "infer")
print(df)
Output:
2014-03-01 2014-06-01 2014-09-01 2015-03-01
0 0.000000 1.0 2.0 3.967391
1 -0.016457 1.0 2.0 4.000000
2 1.000000 2.0 3.0 4.967391
The results are now not nice, round integers anymore, because the amount of days in three months differ.