I'm stuck with a simple DataFrame.reindex().interpolate()
problem because the dataframes I am using don't have a datetime index.
I have DataFrame1: t
that looks like this:
In[1]: import pandas as pd
t = pd.DataFrame({'D18O': [-0.47, -0.12, 0.55, 0.72, 1.8 , 1.1 , 0.43, -0.29, -0.55,
-0.6 , -0.32, 0.28, 0.72, 1.1 , 1.34, 1.32, 1.11, 0.46,
0.09, 0.02]})
Out[2]:
1 -0.47
2 -0.12
3 0.55
4 0.72
5 1.80
6 1.10
7 0.43
8 -0.29
9 -0.55
10 -0.60
11 -0.32
12 0.28
13 0.72
14 1.10
15 1.34
16 1.32
17 1.11
18 0.46
19 0.09
20 0.02
Name: D18O, dtype: float64
I want to "stretch" it to 430 rows by evenly spacing each row and linearly interpolating values in between. This is because my DataFrame2: env
has 430 rows and I want to do some later analysis that needs both frames to have the same dimension.
In[2]: env.index
Out[49]: RangeIndex(start=0, stop=430, step=1)
I've tried reindexing and interpolating in many combinations but just can't find the right method. I think the problem is, that 430 is not evenly divisable by 19/20.
new_idx = np.linspace(t.index[0], t.index[-1], env.shape[0])
t.reindex(new_idx).interpolate()
I thought this qould work, but because the indexes are not even it skips most of the values in t
and leaves me with a nearly empty new dataframe.
For the reindexing step I expect something like:
In[3]: t['D18O']
Out[3]:
0 0.47
2.13157 NaN
2.26315 NaN
... ...
21.5 -0.12
22.63157 NaN
23.76315 NaN
... ...
... ...
430 0.02
Name: D18O, dtype: float64
The indexes don't really matter, as long as the values are evenly spaced and the number of rows matches the number of rows in env
.