예제 #1
0
    def test__timeseriescompat_multiple(self):
        "Tests the compatibility of multiple time series."
        seriesM_10 = time_series(N.arange(10),
                                    date_array(
                                      start_date=Date(freq='m', year=2005, month=1),
                                      length=10)
                                )

        seriesD_10 = time_series(N.arange(10),
                                    date_array(
                                      start_date=Date(freq='d', year=2005, month=1, day=1),
                                      length=10)
                                )

        seriesD_5 = time_series(N.arange(5),
                                    date_array(
                                      start_date=Date(freq='d', year=2005, month=1, day=1),
                                      length=5)
                                )

        seriesD_5_apr = time_series(N.arange(5),
                                    date_array(
                                      start_date=Date(freq='d', year=2005, month=4, day=1),
                                      length=5)
                                )

        assert(tseries._timeseriescompat_multiple(seriesM_10, seriesM_10, seriesM_10))

        try:
            tseries._timeseriescompat_multiple(seriesM_10, seriesD_10)
            exception = False
        except:
            exception = True
        assert(exception)

        try:
            tseries._timeseriescompat_multiple(seriesD_5, seriesD_10)
            exception = False
        except:
            exception = True
        assert(exception)

        try:
            tseries._timeseriescompat_multiple(seriesD_5, seriesD_5_apr)
            exception = False
        except:
            exception = True
        assert(exception)
예제 #2
0
 def test_fill_missing_dates(self):
     """Test fill_missing_dates function"""
     _start = Date(freq='m', year=2005, month=1)
     _end = Date(freq='m', year=2005, month=4)
     #
     dates = date_array([_start, _end], freq='M')
     series = time_series([1, 2], dates)
     filled_ser = fill_missing_dates(series)
     #
     assert_equal(filled_ser.start_date, _start)
     assert_equal(filled_ser.end_date, _end)
     assert(filled_ser.isfull())
     assert(not filled_ser.has_duplicated_dates())
     assert_equal(filled_ser.size, _end - _start + 1)
     #
     data = N.arange(5*24).reshape(5,24)
     datelist = ['2007-07-01','2007-07-02','2007-07-03','2007-07-05','2007-07-06']
     dates = date_array_fromlist(datelist, 'D')
     dseries = time_series(data, dates)
     ndates = date_array_fromrange(start_date=dates[0],end_date=dates[-2])
     #
     fseries = fill_missing_dates(dseries)
     assert_equal(fseries.shape, (6,24))
     assert_equal(fseries._mask[:,0], [0,0,0,1,0,0])
     #
     fseries = fill_missing_dates(dseries[:,0])
     assert_equal(fseries.shape, (6,))
     assert_equal(fseries._mask, [0,0,0,1,0,0])
     #
     series = time_series(data.ravel()[:4].reshape(2,2),dates=dates[:-1])
     fseries = fill_missing_dates(series)
     assert_equal(fseries.shape, (5,))
     assert_equal(fseries._mask, [0,0,0,1,0,])
예제 #3
0
 def test_on2d(self):
     "Tests getitem on a 2D series"
     (a,b,d) = ([1,2,3],[3,2,1], date_array(thisday('M'),length=3))
     ser_x = time_series(N.column_stack((a,b)), dates=d)
     assert_equal(ser_x[0,0], time_series(a[0],d[0]))
     assert_equal(ser_x[0,:], time_series([(a[0],b[0])], d[0]))
     assert_equal(ser_x[:,0], time_series(a, d))
     assert_equal(ser_x[:,:], ser_x)
예제 #4
0
 def test_pickling(self):
     "Tests pickling/unpickling"
     (series, data, dates) = self.d
     import cPickle
     series_pickled = cPickle.loads(series.dumps())
     assert_equal(series_pickled._dates, series._dates)
     assert_equal(series_pickled._data, series._data)
     assert_equal(series_pickled._mask, series._mask)
     #
     data = masked_array(N.matrix(range(10)).T, mask=[1,0,0,0,0]*2)
     dates = date_array(start_date=thisday('D'), length=10)
     series = time_series(data,dates=dates)
     series_pickled = cPickle.loads(series.dumps())
     assert_equal(series_pickled._dates, series._dates)
     assert_equal(series_pickled._data, series._data)
     assert_equal(series_pickled._mask, series._mask)
     assert(isinstance(series_pickled._data, N.matrix))
예제 #5
0
02.02.2004;20;-999
02.02.2004;21;57.12
02.02.2004;22;338.3
02.02.2004;23;320.62
02.02.2004;24;110.84}}}

'''* END SAMPLE DATA *'''

{{{
#!python
import numpy as N
import maskedarray as M
import timeseries as ts
data = N.loadtxt("tmp.txt", dtype='|S10', skiprows=2)
dates = ts.date_array([ts.Date(freq='H',string="%s %s:00" %
(d[0],int(d[1])-1))
                       for d in data],
                      freq='H')
series = ts.time_series(data[:,-1].astype(N.float_),
                        dates,
                        mask=(data[:,-1]=='-999'))

# <markdowncell>

# ### frequencies
# 
# #### Question
# 
# Is there a example data set for at least one year on a high temporal
# resolution: 15min or at least 1h. Having such a common data set one
# could set up tutorials examples and debug or ask questions easier
# because all will have the same (non-confidetial) data on the disk.
예제 #6
0
def _daily_finder(vmin, vmax, freq, aslocator):

    if freq == _c.FR_BUS:
        periodsperyear = 261
        periodspermonth = 19
    elif freq == _c.FR_DAY:
        periodsperyear = 365
        periodspermonth = 28
    elif get_freq_group(freq) == _c.FR_WK:
        periodsperyear = 52
        periodspermonth = 3
    else:
        raise ValueError("unexpected frequency")

    (vmin, vmax) = (int(vmin), int(vmax))
    span = vmax - vmin + 1
    dates = date_array(start_date=Date(freq,vmin),
                       end_date=Date(freq, vmax))
    default = numpy.arange(vmin, vmax+1)
    # Initialize the output
    if not aslocator:
        format = numpy.empty(default.shape, dtype="|S10")
        format.flat = ''

    def first_label(label_flags):
        if label_flags[0] == 0: return label_flags[1]
        else: return label_flags[0]

    # Case 1. Less than a month
    if span <= periodspermonth:
        month_start = period_break(dates,'month')
        if aslocator:
            major = default[month_start]
            minor = default
        else:
            year_start = period_break(dates,'year')
            format[:] = '%d'
            format[month_start] = '%d\n%b'
            format[year_start] = '%d\n%b\n%Y'
            if not has_level_label(year_start):
                if not has_level_label(month_start):
                    if dates.size > 1:
                        idx = 1
                    else:
                        idx = 0
                    format[idx] = '%d\n%b\n%Y'
                else:
                    format[first_label(month_start)] = '%d\n%b\n%Y'
    # Case 2. Less than three months
    elif span <= periodsperyear//4:
        month_start = period_break(dates,'month')
        if aslocator:
            major = default[month_start]
            minor = default
        else:
            week_start = period_break(dates,'week')
            year_start = period_break(dates,'year')

            format[week_start] = '%d'
            format[month_start] = '\n\n%b'
            format[year_start] = '\n\n%b\n%Y'
            if not has_level_label(year_start):
                if not has_level_label(month_start):
                    format[first_label(week_start)] = '\n\n%b\n%Y'
                else:
                    format[first_label(month_start)] = '\n\n%b\n%Y'
    # Case 3. Less than 14 months ...............
    elif span <= 1.15 * periodsperyear:

        if aslocator:
            d_minus_1 = dates-1

            month_diff = numpy.abs(dates.month - d_minus_1.month)
            week_diff = numpy.abs(dates.week - d_minus_1.week)
            minor_idx = (month_diff + week_diff).nonzero()[0]

            major = default[month_diff != 0]
            minor = default[minor_idx]
        else:
            year_start = period_break(dates,'year')
            month_start = period_break(dates,'month')

            format[month_start] = '%b'
            format[year_start] = '%b\n%Y'
            if not has_level_label(year_start):
                format[first_label(month_start)] = '%b\n%Y'
    # Case 4. Less than 2.5 years ...............
    elif span <= 2.5 * periodsperyear:
        year_start = period_break(dates,'year')
        if aslocator:
            month_start = period_break(dates, 'quarter')
            major = default[year_start]
            minor = default[month_start]
        else:
            quarter_start = period_break(dates, 'quarter')
            format[quarter_start] = '%b'
            format[year_start] = '%b\n%Y'
    # Case 4. Less than 4 years .................
    elif span <= 4 * periodsperyear:
        year_start = period_break(dates,'year')
        month_start = period_break(dates, 'month')
        if aslocator:
            major = default[year_start]
            minor = default[month_start]
        else:
            month_break = dates[month_start].month
            jan_or_jul = month_start[(month_break == 1) | (month_break == 7)]
            format[jan_or_jul] = '%b'
            format[year_start] = '%b\n%Y'
    # Case 5. Less than 11 years ................
    elif span <= 11 * periodsperyear:
        year_start = period_break(dates,'year')
        if aslocator:
            quarter_start = period_break(dates, 'quarter')
            major = default[year_start]
            minor = default[quarter_start]
        else:
            format[year_start] = '%Y'
    # Case 6. More than 12 years ................
    else:
        year_start = period_break(dates,'year')
        year_break = dates[year_start].years
        nyears = span/periodsperyear
        (min_anndef, maj_anndef) = _get_default_annual_spacing(nyears)
        major_idx = year_start[(year_break % maj_anndef == 0)]
        if aslocator:
            major = default[major_idx]
            minor_idx = year_start[(year_break % min_anndef == 0)]
            minor = default[minor_idx]
        else:
            format[major_idx] = '%Y'
    #............................................
    if aslocator:
        return minor, major
    else:
        formatted = (format != '')
        return dict([(d,f) for (d,f) in zip(default[formatted],format[formatted])])
예제 #7
0
    else:
        fig = tsfigure(series=series)
    # Get the current axe, or create one
    sub = fig._axstack()
    if sub is None:
        sub = fig.add_tsplot(111,series=series,**kwargs)
    try:
        ret = sub.tsplot(series, *args, **kwargs)
        pylab.draw_if_interactive()
    except:
        pylab.hold(b)
        raise
    pylab.hold(b)
    return ret

################################################################################
if __name__ == '__main__':

    da = date_array(start_date=Date(freq='B', year=2003, quarter=3, month=1, day=17),
                    length=10)
    ser = timeseries.time_series(MA.arange(len(da)), dates=da)
#    ser[4] = MA.masked
#    ser_2 = timeseries.time_series(MA.arange(len(da)), dates=da.asfreq('Q'))

    pylab.figure()
    pylab.gcf().add_tsplot(111)
    pylab.gca().tsplot(ser, 'ko-')
    pylab.gca().format_dateaxis()
#    pylab.gca().tsplot(ser_2, 'rs')
    pylab.show()
예제 #8
0
    def __call__(self, *tseries, **kwargs):
        """generate a report

:Paramaters:
    - `*tseries` : the TimeSeries objects to be used in the report. If
      omitted, the previously set TimeSeries objects will be used
    - `**kwargs` : the options to be used in the report. See the __doc__
      string for the Report class for valid options. If omitted, the
      previously set options will be used"""

        option_dict = self.__make_dict(**kwargs)
        if len(tseries) == 0:
            tseries = self.tseries
        
        def option(kw):
            return option_dict.get(kw, _default_options[kw])
            
        dates = option('dates')
        header_row = option('header_row')
        header_char = option('header_char')
        header_justify = option('header_justify')
        row_char = option('row_char')
        footer_label = option('footer_label')
        footer_char = option('footer_char')
        footer_func = option('footer_func')
        delim = option('delim')
        justify = option('justify')
        prefix = option('prefix')
        postfix = option('postfix')
        mask_rep = option('mask_rep')
        datefmt = option('datefmt')
        fmtfunc = option('fmtfunc')
        wrapfunc = option('wrapfunc')
        col_width = option('col_width')
        nls=option('nls')
        output=option('output')
        fixed_width=option('fixed_width')
        
        if header_row is not None:
            has_header=True
            if len(header_row) == len(tseries)+1:
                # label for date column included
                rows = [header_row]
            elif len(header_row) == len(tseries):
                # label for date column not included
                rows = [['']+header_row]
            else:
                raise ValueError("mismatch with number of headers and series")
        else:
            has_header=False
            rows=[]

        if fixed_width:
        
            def _standardize_justify(userspec):
                if isinstance(userspec, str):
                    # justify all columns the the same way
                    return [userspec for x in range(len(tseries)+1)]
                elif isinstance(userspec, list):
                    if len(userspec) == len(tseries):
                        # justification for date column not included, so set that
                        # to left by default
                        return ['left'] + userspec
                else:
                    raise ValueError("invalid `justify` specification")
                    
            if justify is not None:
                justify = _standardize_justify(justify)
            else:
                # default column justification
                justify = ['left']
                for ser in tseries:
                    if ser.dtype.char in 'SUO': justify.append('left')
                    else: justify.append('right')
                    
                    
            if header_justify is not None:
                header_justify = _standardize_justify(header_justify)
            else:
                # default column justification
                header_justify = ['left' for x in range(len(tseries)+1)]
        else:
            justify = [None for x in range(len(tseries)+1)]

        if datefmt is None:
            def datefmt_func(date): return str(date)
        else:
            def datefmt_func(date): return date.strfmt(datefmt)

        if dates is None:
            tseries = ts.align_series(*tseries)
            dates = ts.date_array(start_date=tseries[0].start_date,
                                  end_date=tseries[0].end_date)
        else:
            tseries = ts.align_series(start_date=dates[0], end_date=dates[-1], *tseries)

        if isinstance(fmtfunc, list):
            fmtfunc = [fmtfunc_wrapper(f, mask_rep) for f in fmtfunc]
        else:
            fmtfunc = [fmtfunc_wrapper(fmtfunc, mask_rep)]*len(tseries)

        def wrapfunc_default(func):
            if func is None: return lambda x:x
            else: return func
            
        if isinstance(wrapfunc, list):
            if len(wrapfunc) == len(tseries):
                wrapfunc = [lambda x: x] + wrapfunc
            wrapfunc = [wrapfunc_default(func) for func in wrapfunc]
        else:
            wrapfunc = [wrapfunc_default(wrapfunc) for x in range(len(tseries)+1)]
    
            
        if isinstance(col_width, list):
            if len(col_width) == len(tseries):
                col_width = [None] + col_width
        else:
            col_width = [col_width for x in range(len(tseries)+1)]

        def getval(series, date):
            try:
                val = series[date]
            except IndexError:
                val = ma.masked
            return val

        for d in dates:
            rows.append([datefmt_func(d)]+[fmtfunc[i](getval(ser, d)) for i, ser in enumerate(tseries)])

        if footer_func is not None:
            has_footer=True
            if not isinstance(footer_func, list):
                footer_func = [footer_func]*len(tseries)

            if footer_label is None: footer_label = ['']
            else: footer_label = [footer_label]

            footer_data = []
            for i, ser in enumerate(tseries):
                if footer_func[i] is None:
                    footer_data.append('')
                else:
                    footer_data.append(fmtfunc[i](footer_func[i](ser[dates])))

            rows.append(footer_label + footer_data)
        else:
            has_footer=False
            
            
        def rowWrapper(row):
            newRows = [wrapfunc[i](item).split('\n') for i, item in enumerate(row)]
            return [[(substr or '') for substr in item] for item in map(None,*newRows)]
        # break each logical row into one or more physical ones
        logicalRows = [rowWrapper(row) for row in rows]
        numLogicalRows = len(logicalRows)
        # columns of physical rows
        columns = map(None,*reduce(operator.add,logicalRows))
        numCols = len(columns)
        colNums = list(range(numCols))

        # get the maximum of each column by the string length of its items
        maxWidths = [max(col_width[i], *[len(str(item)) for item in column])
                        for i, column in enumerate(columns)]

        def getSeparator(char, separate):
            if char is not None and separate:
                return char * (len(prefix) + len(postfix) + sum(maxWidths) + \
                                             len(delim)*(len(maxWidths)-1))
            else:
                return None

        header_separator = getSeparator(header_char, has_header)
        footer_separator = getSeparator(footer_char, has_footer)
        row_separator = getSeparator(row_char, True)

        # select the appropriate justify method
        justify_funcs = {'center':str.center, 'right':str.rjust, 'left':str.ljust,
                          'none':(lambda text, width: text)}

        if has_header and has_footer:
            data_start = 1
            data_end = numLogicalRows-3
        elif has_header:
            data_start = 1
            data_end = numLogicalRows-2
        elif has_footer:
            data_start = 0
            data_end = numLogicalRows-3
        else:
            data_start = 0
            data_end = numLogicalRows-2

        for rowNum, physicalRows in enumerate(logicalRows):
            for row in physicalRows:
                if rowNum == 0 and header_separator:
                    _justify = header_justify
                else:
                    _justify = justify
                    
                output.write(prefix \
                           + delim.join([justify_funcs[str(_justify[colNum]).lower()](str(item),width) for (colNum,item,width) in zip(colNums,row,maxWidths)]) \
                           + postfix + nls)

            if row_separator and (data_start <= rowNum <= data_end):
                output.write(row_separator + nls)
            elif header_separator and rowNum < data_start:
                output.write(header_separator + nls)
            elif footer_separator and rowNum == data_end + 1:
                output.write(footer_separator + nls)
예제 #9
0
# setup all the data to be used for reading and writing
data = {'dates':{}, 'darrays':{}, 'freqs':{}, 'cser':{}, 'tser':{}, 'scalars':{}}

data['dates']['a'] = ts.Date(freq='A', year=2004)
data['dates']['q'] = ts.Date(freq='Q', year=2004, quarter=1)
data['dates']['m'] = ts.Date(freq='M', year=2004, month=1)
data['dates']['w'] = ts.Date(freq='W', year=2004, month=1, day=1)
data['dates']['b'] = ts.Date(freq='b', year=2004, month=1, day=1)
data['dates']['d'] = ts.Date(freq='d', year=2004, month=1, day=1)
data['dates']['h'] = ts.Date(freq='h', year=2004, month=1, day=1, hour=0)
data['dates']['t'] = ts.Date(freq='t', year=2004, month=1, day=1, hour=0, minute=0)
data['dates']['s'] = ts.Date(freq='s', year=2004, month=1, day=1, hour=0, minute=0, second=0)

for freq in data['dates']:
    data['darrays'][freq] = ts.date_array(start_date=data['dates'][freq], length=10)
    data['cser']['date_'+freq] = data['darrays'][freq]

data['cser']['bool'] = [True, False, True, False, True, True]
data['cser']['int32'] = np.arange(6).astype(np.int32)
data['cser']['int64'] = np.arange(6).astype(np.int64)
data['cser']['float32'] = np.arange(6).astype(np.float32)
data['cser']['float64'] = np.arange(6).astype(np.float64)
data['cser']['str'] = ["asdf", "aasssssssss", "zzzzzzzzzzzz", "", "blah"]

for x in data['cser']:
    data['cser'][x] = ma.masked_array(data['cser'][x])
    data['tser'][x] = ts.time_series(data['cser'][x], start_date=data['dates']['a'])

for freq in data['dates']:
    data['freqs'][freq] = ts.time_series(np.arange(20).astype(np.float32), start_date=data['dates'][freq])