Changeset 1151

Show
Ignore:
Timestamp:
07/20/08 20:45:42 (4 months ago)
Author:
mattknox_ca
Message:

performance improvements for scalar indexing

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/timeseries/scikits/timeseries/tdates.py

    r1149 r1151  
    224224            indx = self.find_dates(indx) 
    225225            reset_full = False 
     226        elif isinstance(indx, slice): 
     227            pass 
    226228        elif np.asarray(indx).dtype.kind == 'O': 
    227229            try: 
     
    229231            except AttributeError: 
    230232                pass 
     233 
    231234        # Select the data 
    232235        r = ndarray.__getitem__(self, indx) 
     
    246249        else: 
    247250            if hasattr(r, '_cachedinfo'): 
     251 
    248252                _cache = r._cachedinfo 
    249253                _cache.update(dict([(k,_cache[k][indx]) 
     
    255259                    _cache['hasdups'] = None 
    256260            return r 
    257  
    258     def __getslice__(self, i, j): 
    259         r = ndarray.__getslice__(self, i, j) 
    260         if hasattr(r, '_cachedinfo'): 
    261             _cache = r._cachedinfo 
    262             _cache.update(dict([(k,_cache[k][i:j]) 
    263                                 for k in ('toobj', 'tostr', 'toord') 
    264                                 if _cache[k] is not None])) 
    265             _cache['steps'] = None 
    266         return r 
    267261 
    268262    def __repr__(self): 
     
    475469    def date_to_index(self, dates): 
    476470        "Returns the index corresponding to one given date, as an integer." 
    477         vals = self.tovalue() 
     471        vals = self.view(ndarray) 
     472        if isinstance(dates, Date): 
     473            _val = dates.value 
     474            if _val not in vals: 
     475                raise IndexError("Date '%s' is out of bounds" % dates) 
     476            if self.isvalid(): 
     477                return _val - vals[0] 
     478            else: 
     479                return np.where(vals == _val)[0][0] 
     480 
    478481        _dates = DateArray(dates, freq=self.freq) 
    479  
    480         if _dates.size == 1 and np.array(dates, dtype=int_).ndim == 0: 
    481             scalar = True 
    482         else: 
    483             scalar = False 
    484  
    485482        if self.isvalid(): 
    486             indx = (_dates.tovalue() - vals[0]) 
     483            indx = (_dates.view(ndarray) - vals[0]) 
    487484            err_cond = (indx < 0) | (indx > self.size) 
    488485            if err_cond.any(): 
     
    490487                err_msg = "Date '%s' is out of bounds '%s' <= date <= '%s'" 
    491488                raise IndexError(err_msg % (err_indx, self[0], self[-1])) 
    492             if scalar: 
    493                 return indx.item() 
    494             return indx.tolist() 
    495         indx = [vals.tolist().index(d) for d in _dates.tovalue()] 
    496  
    497         if scalar: 
    498             # dates was a single date, so do scalar indexing 
    499             return indx[0] 
     489            return indx 
     490        vals = vals.tolist() 
     491        indx = np.array([vals.index(d) for d in _dates.view(ndarray)]) 
     492 
    500493        return indx 
    501494 
  • trunk/timeseries/scikits/timeseries/tests/test_dates.py

    r1124 r1151  
    913913        chosen = dates.date_to_index(choices[:-1]) 
    914914        assert_equal(chosen, [2, 4, 6, 8]) 
    915         assert(isinstance(chosen, list)) 
     915        assert(isinstance(chosen, np.ndarray)) 
    916916        # 
    917917        try: 
     
    956956        chosen = dates_invalid.date_to_index(choices[:-1]) 
    957957        assert_equal(chosen, [2, 4, 6, 8]) 
    958         assert(isinstance(chosen, list)) 
     958        assert(isinstance(chosen, np.ndarray)) 
    959959        # 
    960960        try: