Skip to content

Commit 4836cdf

Browse files
committed
Fix performance regression in Series.asof by avoiding pre-computing nulls and returning value by indexing the underlying ndarray.
1 parent 233d51d commit 4836cdf

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

pandas/core/generic.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -3735,10 +3735,10 @@ def asof(self, where, subset=None):
37353735
if not self.index.is_monotonic:
37363736
raise ValueError("asof requires a sorted index")
37373737

3738-
if isinstance(self, ABCSeries):
3738+
is_series = isinstance(self, ABCSeries)
3739+
if is_series:
37393740
if subset is not None:
37403741
raise ValueError("subset is not valid for Series")
3741-
nulls = self.isnull()
37423742
elif self.ndim > 2:
37433743
raise NotImplementedError("asof is not implemented "
37443744
"for {type}".format(type(self)))
@@ -3747,7 +3747,6 @@ def asof(self, where, subset=None):
37473747
subset = self.columns
37483748
if not is_list_like(subset):
37493749
subset = [subset]
3750-
nulls = self[subset].isnull().any(1)
37513750

37523751
if not is_list_like(where):
37533752
start = self.index[0]
@@ -3761,13 +3760,21 @@ def asof(self, where, subset=None):
37613760
loc = self.index.searchsorted(where, side='right')
37623761
if loc > 0:
37633762
loc -= 1
3764-
while nulls[loc] and loc > 0:
3763+
3764+
if is_series:
3765+
values = self._values
3766+
while loc > 0 and isnull(values[loc]):
3767+
loc -= 1
3768+
return values[loc]
3769+
3770+
while loc > 0 and self.loc[loc, subset].isnull().any():
37653771
loc -= 1
37663772
return self.iloc[loc]
37673773

37683774
if not isinstance(where, Index):
37693775
where = Index(where)
37703776

3777+
nulls = self.isnull() if is_series else self[subset].isnull().any(1)
37713778
locs = self.index.asof_locs(where, ~(nulls.values))
37723779

37733780
# mask the missing

0 commit comments

Comments
 (0)