Changeset 1151
- Timestamp:
- 07/20/08 20:45:42 (4 months ago)
- Files:
-
- trunk/timeseries/scikits/timeseries/tdates.py (modified) (6 diffs)
- trunk/timeseries/scikits/timeseries/tests/test_dates.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/timeseries/scikits/timeseries/tdates.py
r1149 r1151 224 224 indx = self.find_dates(indx) 225 225 reset_full = False 226 elif isinstance(indx, slice): 227 pass 226 228 elif np.asarray(indx).dtype.kind == 'O': 227 229 try: … … 229 231 except AttributeError: 230 232 pass 233 231 234 # Select the data 232 235 r = ndarray.__getitem__(self, indx) … … 246 249 else: 247 250 if hasattr(r, '_cachedinfo'): 251 248 252 _cache = r._cachedinfo 249 253 _cache.update(dict([(k,_cache[k][indx]) … … 255 259 _cache['hasdups'] = None 256 260 return r 257 258 def __getslice__(self, i, j):259 r = ndarray.__getslice__(self, i, j)260 if hasattr(r, '_cachedinfo'):261 _cache = r._cachedinfo262 _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'] = None266 return r267 261 268 262 def __repr__(self): … … 475 469 def date_to_index(self, dates): 476 470 "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 478 481 _dates = DateArray(dates, freq=self.freq) 479 480 if _dates.size == 1 and np.array(dates, dtype=int_).ndim == 0:481 scalar = True482 else:483 scalar = False484 485 482 if self.isvalid(): 486 indx = (_dates. tovalue() - vals[0])483 indx = (_dates.view(ndarray) - vals[0]) 487 484 err_cond = (indx < 0) | (indx > self.size) 488 485 if err_cond.any(): … … 490 487 err_msg = "Date '%s' is out of bounds '%s' <= date <= '%s'" 491 488 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 500 493 return indx 501 494 trunk/timeseries/scikits/timeseries/tests/test_dates.py
r1124 r1151 913 913 chosen = dates.date_to_index(choices[:-1]) 914 914 assert_equal(chosen, [2, 4, 6, 8]) 915 assert(isinstance(chosen, list))915 assert(isinstance(chosen, np.ndarray)) 916 916 # 917 917 try: … … 956 956 chosen = dates_invalid.date_to_index(choices[:-1]) 957 957 assert_equal(chosen, [2, 4, 6, 8]) 958 assert(isinstance(chosen, list))958 assert(isinstance(chosen, np.ndarray)) 959 959 # 960 960 try:
