Changeset 2100

Show
Ignore:
Timestamp:
02/26/09 13:45:53 (3 years ago)
Author:
pierregm
Message:

* introduced convert_to_annual
* fixed frequency in _extrema
* doc update

Location:
trunk/timeseries/scikits/timeseries
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • trunk/timeseries/scikits/timeseries/doc/source/conf.py

    r2083 r2100  
    222222intersphinx_mapping = {'http://docs.python.org/dev': None, 
    223223                       'http://docs.scipy.org/doc/numpy': None, 
     224                       'http://docs.scipy.org/doc/scipy/reference': None, 
    224225                       'http://matplotlib.sourceforge.net': None} 
    225226 
  • trunk/timeseries/scikits/timeseries/doc/source/core.timeseries.conversion.rst

    r2068 r2100  
    5050 
    5151.. warning:: 
    52    Be careful not to confuse the two methods :meth:`asfreq` and :meth:`convert`. 
     52   Be careful not to confuse the two methods :meth:`~TimeSeries.asfreq`  
     53   and :meth:`~TimeSeries.convert`. 
    5354 
    5455   * :meth:`~TimeSeries.asfreq` simply takes every date 
     
    6263     places the data from the original series into appropriate points in the new 
    6364     series. 
     65 
     66 
     67.. autosummary:: 
     68   :toctree: generated/ 
     69 
     70   convert 
     71   scikits.timeseries.extras.convert_to_annual 
  • trunk/timeseries/scikits/timeseries/extras.py

    r2080 r2100  
    2424 
    2525__all__ = ['accept_atmost_missing', 
    26            'count_missing', 
     26           'convert_to_annual', 'count_missing', 
    2727           'guess_freq', 
    2828           'isleapyear', 
     
    9797        raise NotImplementedError, "Not yet implemented for that frequency..." 
    9898    return missing 
     99 
     100 
     101 
     102def convert_to_annual(series): 
     103    """ 
     104    Group a series by years, taking leap years into account. 
     105     
     106    The output has as many rows as distinct years in the original series, 
     107    and as many columns as the length of a leap year in the units corresponding 
     108    to the original frequency (366 for daily frequency, 366*24 for hourly...). 
     109    The fist column of the output corresponds to Jan. 1st, 00:00:00, 
     110    while the last column corresponds to Dec, 31st, 23:59:59. 
     111    Entries corresponding to Feb. 29th are masked for non-leap years. 
     112 
     113    For example, if the initial series has a daily frequency, the 59th column 
     114    of the output always corresponds to Feb. 28th, the 61st column to Mar. 1st, 
     115    and the 60th column is masked for non-leap years. 
     116    With a hourly initial frequency, the (59*24)th column of the output always 
     117    correspond to Feb. 28th 23:00, the (61*24)th column to Mar. 1st, 00:00, and 
     118    the 24 columns between (59*24) and (61*24) are masked. 
     119 
     120    If the original frequency is less than daily, the output is equivalent to 
     121    ``series.convert('A', func=None)``. 
     122 
     123 
     124    Parameters 
     125    ---------- 
     126    series : TimeSeries 
     127        A valid :class:`~scikits.timeseries.TimeSeries` object. 
     128 
     129    Returns 
     130    ------- 
     131    aseries : TimeSeries 
     132        A 2D  :class:`~scikits.timeseries.TimeSeries` object with annual ('A') 
     133        frequency. 
     134     
     135    """ 
     136    dates = series.dates 
     137    if dates.freq < _c.FR_DAY: 
     138        return series.convert('A') 
     139    idx0228 = dates.date_to_index(Date(dates.freq, 
     140                                       year=dates[0].year, month=2, day=28, 
     141                                       hour=00, minute=00, second=00)) 
     142    idx0301 = dates.date_to_index(Date(dates.freq, 
     143                                       year=dates[0].year, month=3, day=1, 
     144                                       hour=00, minute=00, second=00)) 
     145    aseries = series.convert('A') 
     146    leapcondition = isleapyear(aseries.dates.years) 
     147    leapidx = np.arange(len(aseries), dtype=int)[~leapcondition] 
     148    aseries[leapidx, idx0301:] = aseries[leapidx, idx0228:idx0228-idx0301] 
     149    aseries[leapidx, idx0228:idx0301] = ma.masked 
     150    return aseries 
     151 
     152 
    99153 
    100154#............................................................................. 
  • trunk/timeseries/scikits/timeseries/lib/plotlib.py

    r2068 r2100  
    995995        """ 
    996996    Plots the data parsed in argument to the current axes. 
    997     This command accepts the same optional keywords as :func:`matplotlib.plot`. 
     997    This command accepts the same optional keywords as :func:`matplotlib.pyplot.plot`. 
    998998 
    999999    The argument ``args`` is a variable length argument, allowing for multiple 
     
    10141014 
    10151015    a :class:`~scikits.timeseries.TimeSeries` object or one of its subclass 
    1016        with or without a format string: 
     1016    with or without a format string: 
    10171017       The given time series is plotted with the given format. 
    10181018       If no format string is given, the default format is used instead. 
  • trunk/timeseries/scikits/timeseries/tests/test_dates.py

    r2073 r2100  
    123123        _dt = ts.Date(freq='D', datetime=datetime.datetime(2007, 1, 1, 0, 0, 0, 0)) 
    124124        assert_equal(_dt, _tsdt) 
    125         print "finished test_fromsobjects" 
    126125 
    127126 
     
    145144        assert_equal(date_array(n,length=3), d) 
    146145        assert_equal(date_array(n, n+2), d) 
    147         print "finished test_shortcuts" 
    148146 
    149147 
  • trunk/timeseries/scikits/timeseries/tests/test_extras.py

    r2080 r2100  
    7878        assert_equal(result._mask.all(-1), [1,1]) 
    7979 
     80    def test_convert_to_annual(self): 
     81        "Test convert_to_annual" 
     82        base = dict(D=1, H=24, T=24*60, S=24*3600) 
     83        #for fq in ('D', 'H', 'T', 'S'): 
     84        # Don't test for minuTe and Second frequency, too time consuming. 
     85        for fq in ('D', 'H'): 
     86            dates = date_array(start_date=Date(fq, '2001-01-01 00:00:00'), 
     87                               end_date=Date(fq, '2004-12-31 23:59:59')) 
     88            bq = base[fq] 
     89            series = time_series(range(365*bq)*3+range(366*bq), 
     90                                 dates=dates) 
     91            control = ma.masked_all((4, 366*bq), dtype=series.dtype) 
     92            control[0, :58*bq] = range(58*bq) 
     93            control[0, 59*bq:] = range(58*bq, 365*bq) 
     94            control[[1, 2]] = control[0] 
     95            control[3] = range(366*bq) 
     96            test = convert_to_annual(series) 
     97            assert_equal(test, control) 
     98 
    8099 
    81100 
  • trunk/timeseries/scikits/timeseries/tseries.py

    r2068 r2100  
    13051305            _s = np.choose(idx, np.rollaxis(_series,axis,0)) 
    13061306            _d = np.choose(idx, np.rollaxis(_dates,axis,0)) 
    1307             result = time_series(_s, dates=_d) 
     1307            result = time_series(_s, dates=_d, freq=_dates.freq) 
    13081308        return result 
    13091309