[Scipy-svn] r3804 - in branches/testing_cleanup: . scipy/sandbox/timeseries

scipy-svn at scipy.org scipy-svn at scipy.org
Tue Jan 8 17:55:59 EST 2008


Author: matthew.brett at gmail.com
Date: 2008-01-08 16:55:53 -0600 (Tue, 08 Jan 2008)
New Revision: 3804

Modified:
   branches/testing_cleanup/
   branches/testing_cleanup/scipy/sandbox/timeseries/dates.py
   branches/testing_cleanup/scipy/sandbox/timeseries/trecords.py
Log:
Merged revisions 3801 via svnmerge from 
http://svn.scipy.org/svn/scipy/trunk

........
  r3801 | dhuard | 2008-01-08 11:36:52 -0800 (Tue, 08 Jan 2008) | 1 line
  
  Fixed bug in fromtextfile
........



Property changes on: branches/testing_cleanup
___________________________________________________________________
Name: svnmerge-integrated
   - /branches/scipy.scons:1-3533 /trunk:1-3662,3667-3672,3674-3675,3684,3723-3735,3738,3744,3749,3754-3755,3759,3761,3764,3769,3774,3778-3799
   + /branches/scipy.scons:1-3533 /trunk:1-3662,3667-3672,3674-3675,3684,3723-3735,3738,3744,3749,3754-3755,3759,3761,3764,3769,3774,3778-3803

Modified: branches/testing_cleanup/scipy/sandbox/timeseries/dates.py
===================================================================
--- branches/testing_cleanup/scipy/sandbox/timeseries/dates.py	2008-01-08 22:52:05 UTC (rev 3803)
+++ branches/testing_cleanup/scipy/sandbox/timeseries/dates.py	2008-01-08 22:55:53 UTC (rev 3804)
@@ -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: branches/testing_cleanup/scipy/sandbox/timeseries/trecords.py
===================================================================
--- branches/testing_cleanup/scipy/sandbox/timeseries/trecords.py	2008-01-08 22:52:05 UTC (rev 3803)
+++ branches/testing_cleanup/scipy/sandbox/timeseries/trecords.py	2008-01-08 22:55:53 UTC (rev 3804)
@@ -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