Here's a way to do it just with datetime and calendar. It's rather lengthy though, beware.
First, we need a method to make the desired time series
Months and quarters are a bit tricky, which date is one month after January 31, for example?
But a method could look like this:
For testing, I included the generation of random values that belong with the dates.
from datetime import datetime, timedelta, date
import calendar
from random import random
def makeseries(startdate):
datesA = [startdate] # collect the dates in this list
valsA = [random()] # and the randomly generated 'data' in this one
date = startdate
# add days
step = timedelta(1)
while date - startdate <= timedelta(91):
date += step
datesA += [date]
valsA += [random()]
# add months
step = timedelta(30)
while date - startdate <= timedelta(2*365):
if date.month in [1,3,5,7,8,10,12]:
date += timedelta(1)
elif date.month == 2:
date -= timedelta(2)
date += step
datesA += [date]
valsA += [random()]
# add quarters
step = timedelta(91)
while date - startdate <= timedelta(int(365*10)):
date += step
if date.year % 4 == 0:
date += timedelta(1)
datesA += [date]
valsA += [random()]
# add years
step = timedelta(365)
while date - startdate <= timedelta(int(365*50)):
date += step
if date.year % 4 == 0:
date += timedelta(1)
datesA += [date]
valsA += [random()]
return datesA, valsA
Then, a simple method to find the nearest date to a given date in a series of dates
def findIndexOfNearest(series, D):
# returns the index of the date in series that is closest to, but greater than D
for i, date in enumerate(series):
if date > D:
return i
return None
Generate the two time series, plus some mock date for the first series
thisyear = datetime.today().year
quarterEndMonth = (datetime.today().month+2)//3*3
quarterEndDay = calendar.monthrange(thisyear, quarterEndMonth)[1]
d1,v1 = makeseries(date.today())
d2,_ = makeseries(date(thisyear,quarterEndMonth, quarterEndDay))
v2 = []
Interpolate using timedeltas and print the interpolated values
for d in d2:
i = findIndexOfNearest(d1, d)
if i:
prev = d1[i-1]
next = d1[i]
prevRatio = 1-(d-prev).total_seconds()/(next-prev).total_seconds()
nextRatio = 1-(next-d).total_seconds()/(next-prev).total_seconds()
interp = prevRatio*v1[i-1] + nextRatio*v1[i]
v2 += [interp]
print("%s = %.2f * %s + %.2f * %s" % (d, prevRatio, prev, nextRatio, next))
print("%17.2f * %10.2f + %.2f * %10.2f = %.2f" % \
(prevRatio, v1[i-1], nextRatio, v1[i], interp))
else: # date to be interpolated is past last original date
v2 += [v1[-1]]
print("%s = 1.00 * %s = %24.2f" % (d,d1[-1],v1[-1]))
Some example output:
Here, the original series just switched to 3-month gaps, with one date in November, and another in February the next year. The date for which we are interpolating is in December.
original original
date date
v v
2014-12-02 = 0.69 * 2014-11-04 + 0.31 * 2015-02-03
^ 0.69 * 0.95 + 0.31 * 0.10 = 0.69
| ^ ^ ^ ^ ^
| | original | original interpolated
date from | value | value value
2nd series weight weight
pandaspackage offers? They should have some pretty good coverage of time series manipulation (they use the code from thescikits.timeseriesfor that).