Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you interpolate from an array containing datetime objects?

I'm looking for a function analogous to np.interp that can work with datetime objects.

For example:

import datetime, numpy as np
arr1 = np.array([datetime.datetime(2008,1,d) for d in range(1,10)])
arr2 = np.arange(1,10)

np.interp(datetime.datetime(2008,1,5,12),arr1,arr2)

would ideally return 5.5, but numpy raises TypeError: array cannot be safely cast to required type. Is there a nice pythonic way around this?

like image 563
Kieran Hunt Avatar asked Jan 13 '15 16:01

Kieran Hunt


2 Answers

numpy.interp() function expects that arr1 and arr2 are 1D sequences of floats i.e., you should convert the sequence of datetime objects to 1D sequence of floats if you want to use np.interp().

If input data uses the same UTC offset for all datetime objects then you could get a float by subtracting a reference date from all values. It is true if your input is UTC (the offset is always zero):

from datetime import datetime
import numpy as np

arr1 = np.array([datetime(2008, 1, d) for d in range(1, 10)])
arr2 = np.arange(1, 10)

def to_float(d, epoch=arr1[0]):
    return (d - epoch).total_seconds()

f = np.interp(to_float(datetime(2008,1,5,12)), map(to_float, arr1), arr2)
print f # -> 5.5
like image 187
jfs Avatar answered Sep 20 '22 21:09

jfs


You can convert them to timestamps (edited to reflect the use of calendar.timegm to avoid timezone-related pitfalls).

# Python 2.7
import datetime, numpy as np
import calendar

def toTimestamp(d):
  return calendar.timegm(d.timetuple())

arr1 = np.array([toTimestamp(datetime.datetime(2008,1,d)) for d in range(1,10)]) 
arr2 = np.arange(1,10)

result = np.interp(toTimestamp(datetime.datetime(2008,1,5,12)),arr1,arr2)
print result # Prints 5.5
like image 41
rchang Avatar answered Sep 20 '22 21:09

rchang