How to delete data from array and then interpolate over that gap in python?

Viewed 391

So I am looking at a day's worth of data. I am trying to remove the hour 12:00 to 13:00 from the data set, and then interpolate over that hour given the rest of the data.

I am running into two problems:

  1. The way I am removing the data is incorrect and I am not sure how to fix it. It deletes the data, but then shifts the data over again so there in not a gap. This is evident when I graph the data, and there is no gap.

  2. I don't know if I need to use interpolate.interp1d or interpolate.splev.

This is not the exact data I am using, but it is a good example to show what I need to do and what I have tried.

import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
from matplotlib import pylab

day_sec=np.arange(0,86400,1) #24 hours in seconds
ydata=np.exp(-day_sec/30) #random ydata for example

remove_seconds=np.arange(43200,46800,1) #12-13:00 in seconds

last=len(remove_seconds)-1 

for i in range(len(day_sec)): #what I have tried to remove the data, but failed
  if (day_sec[i] >= remove_seconds[0]) and (day_sec[i] <= remove_seconds[last]):
    new_day_sec=np.delete(day_sec,[i],None)
    new_ydata=np.delete(ydata,[i],None)

f=interpolate.interp1d(new_day_sec,new_ydata) #not sure this is correct

Any help is appreciated. Thanks!

1 Answers

You can accomplish this without using a loop. Here's an example with some more "meaningful" y data. (The mean of your current ydata is 0, and the expectation is zero also.)

import numpy as np
from scipy import interpolate

# Step one - build your data and combine it into one 2d array
day_sec = np.arange(0, 86400)
ydata = np.random.randn(day_sec.shape[0]) + np.log(day_sec + 1)
data = np.column_stack((day_sec, ydata))

# Step two - get rid of the slice from 12:13 with a mask
# Remember your array is 0-indexed
remove_seconds = np.arange(43200, 46800)
mask = np.ones(data.shape[0], dtype=bool)
# Mask is now False within `remove_seconds` and True everywhere else.
# This allows us to filter (boolean mask) `data`.
mask[remove_seconds] = False
newdata = data[mask, :]

# Step three - interpolate
# `f` here is just a function, not an array.  You need to input
#     your "dropped" range of seconds to produce predicted values.
f = interpolate.interp1d(newdata[:, 0], newdata[:, 1])
predicted = f(remove_seconds)

# If you want to insert the interpolated data
data[~mask, 1] = predicted

plt.plot(data[:, 0], data[:, 1])

enter image description here

Related