prince - 1 year ago 70
Python Question

# interpolate number sequence

I am trying to complete an uncomplete list of numbers, I couldn't find any pythonic way to do it.
I have a sequence of days from 1 to 31, and for each day, I have a float value.

``````#dictionnary{day: value}
monthvalues = {1: 1.12, 2: 3.24, 3: 2.23, 5: 2.10, 7: 4.97} etc.. to 31st day
``````

but my data is uncomplete, and some days are missing! therefore I want to fill the missing picture mathematically this way:

sample month1:

``````{16: 2.00, 18: 4.00}
#==> I want to add to the dictionnary 17: 3.00
``````

sample month2:

``````{10: 2.00, 14: 4.00}
#==> I want to add to the dictionnary 11: 2.25, 12: 2.50, 13: 2.75
``````

sounds simple but I have litteraly millions of rows to treat from an uncomplete sql database and for the moment I am quite lost in for xrange() loops...
Maybe there is a method in the math lib but I couldn't find it.

EDIT:
I want to interpolate the numbers, but as far as I know, only numpy/scipy have these kind of math functions, and im using Pypy which is not compatible with numpy/scipy.

You just need some simple looping and good old programming logic. The one caveat in this logic is that you need a start and end number in order for it to work. I don't know if that makes sense for your data, but interpolation requires that.

Setup:

``````# Keeps track of the last "seen" day
lastday=0

# Default 1st day if missing
if 1 not in monthvalues:
monthvalues[1] = 1.23 #you need a default

# Default 31st day if missing
if 31 not in monthvalues:
monthvalues[31] = 1.23 #you need a default
``````

Processing:

``````# Loop from 1 to 31
for thisday in range(1,32):

# If we do not encounter thisday in the monthvalues, then skip and keep looping
if thisday not in monthvalues:
continue

# How far ago was the last day seen?
gap = thisday - lastday

# If the last day was more than 1 ago, it means there is at least one day amis
if gap > 1:

# This is the amount of the last "seen" day
last_amt = monthvalues[lastday]

# this is the difference between the current day and the last day
diff = monthvalues[thisday] - last_amt

# This is how much you want to interpolate per day in-between
amt_per_day = diff/gap

# there is a gap of missing days, let's fill them
# Start at 1 because we start at the day after the last seen day
for n in range(1, gap):

# Fill the missing days with an interpolated value
monthvalues[lastday+n] = last_amt + amt_per_day * n

# For the next iteration of the loop, this is the last seen day.
lastday = thisday
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download