[SciPy-User] Matching date lists

Lionel Roubeyrie lroubeyrie@limair.asso...
Thu Feb 4 10:31:22 CST 2010


Hi,
a possible way is to use KDTree by converting datetimes to seconds from the epoch :
######################################
import datetime
import time
from scipy.spatial import KDTree

[~]|87> a
    <87>
[datetime.datetime(2009, 12, 23, 13, 57, 16),
  datetime.datetime(2009, 12, 23, 13, 58, 15),
  datetime.datetime(2009, 12, 23, 13, 59, 14),
  datetime.datetime(2009, 12, 23, 14, 0, 14),
  datetime.datetime(2009, 12, 23, 14, 1, 13),
  datetime.datetime(2009, 12, 23, 14, 2, 13),
  datetime.datetime(2009, 12, 23, 14, 3, 13),
  datetime.datetime(2009, 12, 23, 14, 4, 12),
  datetime.datetime(2009, 12, 23, 14, 5, 12),
  datetime.datetime(2009, 12, 23, 14, 6, 12)]
[~]|88> b
    <88>
[datetime.datetime(2009, 12, 23, 13, 57, 21),
  datetime.datetime(2009, 12, 23, 13, 57, 28),
  datetime.datetime(2009, 12, 23, 13, 57, 37),
  datetime.datetime(2009, 12, 23, 13, 57, 44),
  datetime.datetime(2009, 12, 23, 13, 57, 53),
  datetime.datetime(2009, 12, 23, 13, 58, 2),
  datetime.datetime(2009, 12, 23, 13, 58, 9),
  datetime.datetime(2009, 12, 23, 13, 58, 17),
  datetime.datetime(2009, 12, 23, 13, 58, 25),
  datetime.datetime(2009, 12, 23, 13, 58, 33)]
[~]|89> asec=[time.mktime(i.timetuple()) for i in a]
[~]|90> bsec=[time.mktime(i.timetuple()) for i in b]
[~]|91> atree=KDTree(zip(asec, zeros(len(asec))))
[~]|92> btree=KDTree(zip(bsec, zeros(len(bsec))))
[~]|93> atree.query(btree.data[0] )[1]
    <93> 0
[~]|94> atree.query(btree.data[7] )[1]
    <94> 1
######################################

Yes, you can join space and time \o/






More information about the SciPy-User mailing list