[Scipy-svn] r3801 - trunk/scipy/sandbox/timeseries

scipy-svn@scip... scipy-svn@scip...
Tue Jan 8 13:37:05 CST 2008


Author: dhuard
Date: 2008-01-08 13:36:52 -0600 (Tue, 08 Jan 2008)
New Revision: 3801

Modified:
   trunk/scipy/sandbox/timeseries/dates.py
   trunk/scipy/sandbox/timeseries/trecords.py
Log:
Fixed bug in fromtextfile

Modified: trunk/scipy/sandbox/timeseries/dates.py
===================================================================
--- trunk/scipy/sandbox/timeseries/dates.py	2008-01-08 19:15:40 UTC (rev 3800)
+++ trunk/scipy/sandbox/timeseries/dates.py	2008-01-08 19:36:52 UTC (rev 3801)
@@ -562,6 +562,40 @@
         fcode = _c.FR_UND
     return fcode
 
+def guess_freq_date(dates):
+	"""Tries to estimate the frequency of a sequence of datetime objects."""
+	if not type(dates[0]) is dt.datetime:
+		raise AttributeError, "dates not a sequence of datetime objects."
+
+	sorted_dates = numpy.sort(dates)
+	ddif = numpy.diff(sorted_dates)
+	dset = set(ddif)
+	try:
+		dset.remove(dt.timedelta(0))
+	except:
+		pass
+	res = min(dset)
+	if getattr(res, 'seconds', 0) >= 1:
+		fcode = _c.FR_SEC
+	elif getattr(res, 'seconds', 0) >= 60:
+		fcode = _c.FR_MIN
+	elif getattr(res, 'seconds', 0) >= 60*60:
+		fcode = _c.FR_HR
+	elif getattr(res, 'day', 0) >= 1:
+		fcode = _c.FR_DAY			
+	elif getattr(res, 'day', 0) >= 7:
+		fcode = _c.FR_WK
+	elif getattr(res, 'month', 0) >= 1:
+		fcode = _c.FR_MTH
+	elif getattr(res, 'month', 0) >= 3:
+		fcode = _c.FR_QTR
+	elif getattr(res, 'year', 0) >= 1:
+		fcode = _c.FR_ANN
+	else:
+		warnings.warn("Unable to estimate the frequency! %s" % res.__str__())
+		fcode = _c.FR_UND
+	return fcode
+	
 
 def _listparser(dlist, freq=None):
     "Constructs a DateArray from a list."
@@ -578,7 +612,10 @@
         ords += 1
         #...try to guess the frequency
         if freq is None or freq == _c.FR_UND:
-            freq = guess_freq(ords)
+            freq = guess_freq(ords)
+        if freq == _c.FR_UND:
+        	dtobj = [DateTimeFromString(s) for s in dlist]
+        	freq = guess_freq_date(dtobj)
         #...construct a list of dates
         for s in dlist:
             x = Date(freq, string=s)

Modified: trunk/scipy/sandbox/timeseries/trecords.py
===================================================================
--- trunk/scipy/sandbox/timeseries/trecords.py	2008-01-08 19:15:40 UTC (rev 3800)
+++ trunk/scipy/sandbox/timeseries/trecords.py	2008-01-08 19:36:52 UTC (rev 3801)
@@ -494,10 +494,14 @@
     #
     newdates = __getdates(dates=dates, newdates=newdates, length=nvars,
                           freq=None, start_date=None)
-    return TimeSeriesRecords(_datalist, dates=newdates, dtype=mdescr)
 
+    # Sort the datalist according to newdates._unsorted
+    idx = newdates._unsorted
+    _sorted_datalist = [a[idx] for a in _datalist]
+    return TimeSeriesRecords(_sorted_datalist, dates=newdates, dtype=mdescr)
 
 
+
 ################################################################################
 if __name__ == '__main__':
     import numpy as N



More information about the Scipy-svn mailing list