I have a dataframe with data that I obtained from a device, so that timestamp is not at exact seconds. Like below:
hr
timestamp
2022-11-02 20:23:20.850611 72
2022-11-02 20:23:21.868609 71
2022-11-02 20:23:22.932606 71
2022-11-02 20:23:24.057612 72
2022-11-02 20:23:25.182701 71
2022-11-02 20:23:25.932692 74
2022-11-02 20:23:27.057694 72
2022-11-02 20:23:28.182689 72
2022-11-02 20:23:28.932730 72
2022-11-02 20:23:30.057686 71
2022-11-02 20:23:31.182692 69
2022-11-02 20:23:31.932689 68
2022-11-02 20:23:33.057890 67
2022-11-02 20:23:34.182879 68
2022-11-02 20:23:34.932871 68
2022-11-02 20:23:36.057870 66
2022-11-02 20:23:37.182873 68
2022-11-02 20:23:37.933040 72
2022-11-02 20:23:39.058044 73
2022-11-02 20:23:40.183046 78
2022-11-02 20:23:40.959045 84
2022-11-02 20:23:42.058044 84
2022-11-02 20:23:43.183052 75
2022-11-02 20:23:43.936050 73
2022-11-02 20:23:45.058047 81
I tried to resample with code below:
df_resamp = df.resample('250ms').interpolate('cubic')
print(df_resamp.head(30))
But the result whas like below:
timestamp
2022-11-02 20:23:20.750 NaN
2022-11-02 20:23:21.000 NaN
2022-11-02 20:23:21.250 NaN
2022-11-02 20:23:21.500 NaN
2022-11-02 20:23:21.750 NaN
2022-11-02 20:23:22.000 NaN
2022-11-02 20:23:22.250 NaN
2022-11-02 20:23:22.500 NaN
2022-11-02 20:23:22.750 NaN
2022-11-02 20:23:23.000 NaN
2022-11-02 20:23:23.250 NaN
2022-11-02 20:23:23.500 NaN
2022-11-02 20:23:23.750 NaN
2022-11-02 20:23:24.000 NaN
2022-11-02 20:23:24.250 NaN
2022-11-02 20:23:24.500 NaN
2022-11-02 20:23:24.750 NaN
2022-11-02 20:23:25.000 NaN
2022-11-02 20:23:25.250 NaN
2022-11-02 20:23:25.500 NaN
2022-11-02 20:23:25.750 NaN
2022-11-02 20:23:26.000 NaN
2022-11-02 20:23:26.250 NaN
2022-11-02 20:23:26.500 NaN
2022-11-02 20:23:26.750 NaN
2022-11-02 20:23:27.000 NaN
2022-11-02 20:23:27.250 NaN
2022-11-02 20:23:27.500 NaN
2022-11-02 20:23:27.750 NaN
2022-11-02 20:23:28.000 NaN
I wanted to resample to obtain estimated values at exact seconds and with 250 ms resolution. Why did it happen?
You first need to chose a method to resample, then interpolate the gaps:
df_resamp = df.resample('250ms').mean().interpolate('cubic')
print(df_resamp.head(30))
Output:
hr
timestamp
2022-11-02 20:23:20.750 72.000000
2022-11-02 20:23:21.000 71.738699
2022-11-02 20:23:21.250 71.469228
2022-11-02 20:23:21.500 71.215142
2022-11-02 20:23:21.750 71.000000
...
2022-11-02 20:23:27.750 71.855097
2022-11-02 20:23:28.000 72.000000
Intermediate:
df.resample('250ms').mean()
hr
timestamp
2022-11-02 20:23:20.750 72.0
2022-11-02 20:23:21.000 NaN
2022-11-02 20:23:21.250 NaN
2022-11-02 20:23:21.500 NaN
2022-11-02 20:23:21.750 71.0
...
2022-11-02 20:23:27.750 NaN
2022-11-02 20:23:28.000 72.0