How can I interpolate accelerometer data in pandas into fixed sampling rate?
<p>I have a frame with accelerometer data, collected from an iPhone device. The avg sampling rate is 100Hz, but I'd like to have a fixed one with all the data (x, y, z) interpolated. Is that possible in pandas?</p>

<p>For example, here is the head of my df:</p>

<pre><code>ts          x           y           z
0.006960    -0.075324   -0.175405   0.167105
0.016970    -0.048325   -0.186265   0.180108
0.026949    -0.017635   -0.158964   0.215963
0.036959    -0.097063   -0.063350   0.256945
0.046969    -0.139939   -0.046091   0.179085
...
</code></pre>

<p>What I need it to have a fixed sampling rage of 100Hz, so that the ts looks like:
<code>0.00, 0.01, 0.02, 0.03 ... 0.99, 1.00, 1.01 ...</code></p>

<p>Thank you for you help in advance.</p>

<p>There's probably a more efficient way to do this, but you could use scipy to interpolate each column to the time array of interest, and then create a new dataframe from the interpolated data.</p>

<pre><code>import numpy as np
import pandas as pd
from scipy import interpolate

# define the time points of interest
fs = 100  # sampling rate
T = 1/fs  # period
ts = np.arange(0, 0.05, T)

# create a dictionary of interpolated data
interp_data = {}

# loop through the columns and populate
for key in ['x', 'y', 'z']:
    # fit the univariate spline to the data
    spl = interpolate.UnivariateSpline(df['ts'], df[key])
    # compute interpolated values on new time points
    interp_data[key] = spl(ts)

# convert to data frame
interp_frame = pd.DataFrame(interp_data, index=ts)
interp_frame.index.name = 'ts'
interp_frame.head()
</code></pre>

python pandas