diff --git a/pandas/core/index.py b/pandas/core/index.py index 7b5a6b199bc1b..7d6d044b9d5e2 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -1316,22 +1316,6 @@ def asof(self, label): loc = loc.indices(len(self))[-1] return self[loc] - def asof_locs(self, where, mask): - """ - where : array of timestamps - mask : array of booleans where data is not NA - - """ - locs = self.values[mask].searchsorted(where.values, side='right') - - locs = np.where(locs > 0, locs - 1, 0) - result = np.arange(len(self))[mask].take(locs) - - first = mask.argmax() - result[(locs == 0) & (where < self.values[first])] = -1 - - return result - def order(self, return_indexer=False, ascending=True): """ Return sorted copy of Index diff --git a/pandas/core/series.py b/pandas/core/series.py index e43721e52f17e..ea68252b76daa 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2461,6 +2461,10 @@ def asof(self, where): If there is no good value, NaN is returned. + Note that this is really just a convenient shorthand for `Series.reindex`, + and is equivalent to `s.dropna().reindex(where, method='ffill')` for + an array of dates. + Parameters ---------- where : date or array of dates @@ -2476,29 +2480,18 @@ def asof(self, where): if isinstance(where, compat.string_types): where = datetools.to_datetime(where) - values = self.values - if not hasattr(where, '__iter__'): - start = self.index[0] - if isinstance(self.index, PeriodIndex): - where = Period(where, freq=self.index.freq).ordinal - start = start.ordinal - - if where < start: - return np.nan - loc = self.index.searchsorted(where, side='right') - if loc > 0: - loc -= 1 - while isnull(values[loc]) and loc > 0: - loc -= 1 - return values[loc] - - if not isinstance(where, Index): - where = Index(where) - - locs = self.index.asof_locs(where, notnull(values)) - new_values = com.take_1d(values, locs) - return self._constructor(new_values, index=where).__finalize__(self) + is_scalar = True + where = [where] + else: + is_scalar = False + + ret = self.dropna().reindex(where, method='ffill') + + if is_scalar: + return ret.iloc[0] + else: + return ret def to_timestamp(self, freq=None, how='start', copy=True): """ diff --git a/pandas/tseries/period.py b/pandas/tseries/period.py index 56d7d45120fdc..e10332b5483ee 100644 --- a/pandas/tseries/period.py +++ b/pandas/tseries/period.py @@ -318,26 +318,6 @@ def _to_embed(self, keep_tz=False): def _formatter_func(self): return lambda x: "'%s'" % x - def asof_locs(self, where, mask): - """ - where : array of timestamps - mask : array of booleans where data is not NA - - """ - where_idx = where - if isinstance(where_idx, DatetimeIndex): - where_idx = PeriodIndex(where_idx.values, freq=self.freq) - - locs = self.values[mask].searchsorted(where_idx.values, side='right') - - locs = np.where(locs > 0, locs - 1, 0) - result = np.arange(len(self))[mask].take(locs) - - first = mask.argmax() - result[(locs == 0) & (where_idx.values < self.values[first])] = -1 - - return result - def _array_values(self): return self.asobject