Skip to content

Commit c78d687

Browse files
author
Carlos Souza
committed
BUG #15713 Series.asof return nan when series is all nans
1 parent 676a4e5 commit c78d687

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

pandas/core/generic.py

+3
Original file line numberDiff line numberDiff line change
@@ -3971,6 +3971,9 @@ def asof(self, where, subset=None):
39713971
if not isinstance(where, Index):
39723972
where = Index(where) if is_list else Index([where])
39733973

3974+
if self.isnull().all():
3975+
return pd.Series([np.nan])
3976+
39743977
nulls = self.isnull() if is_series else self[subset].isnull().any(1)
39753978
locs = self.index.asof_locs(where, ~(nulls.values))
39763979

pandas/indexes/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,7 @@ def asof_locs(self, where, mask):
18901890
locs = self.values[mask].searchsorted(where.values, side='right')
18911891

18921892
locs = np.where(locs > 0, locs - 1, 0)
1893+
18931894
result = np.arange(len(self))[mask].take(locs)
18941895

18951896
first = mask.argmax()

pandas/tests/series/test_asof.py

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from pandas import (offsets, Series, notnull,
55
isnull, date_range, Timestamp)
66

7+
from pandas.util.testing import assert_frame_equal, assert_series_equal
8+
79
import pandas.util.testing as tm
810

911
from .common import TestData
@@ -148,3 +150,11 @@ def test_errors(self):
148150
s = Series(np.random.randn(N), index=rng)
149151
with self.assertRaises(ValueError):
150152
s.asof(s.index[0], subset='foo')
153+
154+
# series is all nans
155+
result = Series([np.nan]).asof([0])
156+
157+
expected = Series([np.nan])
158+
159+
assert_series_equal(result, expected)
160+

0 commit comments

Comments
 (0)