Upsample timeseries in pandas DataFrame and interpolate

93 views Asked by At

I have an example DataFrame below where my sampling frequency = 250 Hz (1 sample every 4 ms):

df = 

datetime                  sample_value
2000-01-01 00:00:00.004      56
2000-01-01 00:00:00.008      58
2000-01-01 00:00:00.012      64
2000-01-01 00:00:00.016      61

I'm trying to resample the values above to achieve a sampling frequency of 1000 Hz and interpolate. Can I do this using df.resample() and then interpolate.interp1d() from scipy? If so, how?

Most of the examples I've found do this from months -> days or hours -> min., but I'm not able to get this to work for my use case.

Any help would be greatly appreciated. Thank you!

2

There are 2 answers

0
Panda Kim On

Example Code

import pandas as pd
data1 = {'datetime': ['2000-01-01 00:00:00.004000', '2000-01-01 00:00:00.008000', 
                      '2000-01-01 00:00:00.012000', '2000-01-01 00:00:00.016000'], 
         'sample_value': [56, 58, 64, 61]}
df = pd.DataFrame(data1)
df['datetime'] = pd.to_datetime(df['datetime'])

Code

use asfreq

df.set_index('datetime').asfreq('1ms').interpolate().reset_index()
0
Tusher On

Here I Just resample and interpolate time series data with a specific frequency and interpolation method. and used use df.resample() and interpolate.interp1d() from scipy to resample the values to achieve a sampling frequency of 1000 Hz and interpolate.

import pandas as pd
import numpy as np
from scipy import interpolate

# EX DF
df = pd.DataFrame({
    'datetime': pd.date_range('2000-01-01', periods=4, freq='4ms'),
    'sample_value': [56, 58, 64, 61]
})

# as index
df.set_index('datetime', inplace=True)

# Resampling
resampled = df.resample('1ms').interpolate(method='linear')

# new index with 1000 Hzf
new_index = pd.date_range(df.index[0], df.index[-1], freq='1ms')

# Interpolate using interp1d
f = interpolate.interp1d(resampled.index.astype(np.int64), resampled['sample_value'])
interpolated = pd.DataFrame({'datetime': new_index, 'sample_value': f(new_index.astype(np.int64))})


print(interpolated)