Skip to content

BUG: Series.asof fails for all NaN Series (GH15713) #15758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
3 changes: 3 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3971,6 +3971,9 @@ def asof(self, where, subset=None):
if not isinstance(where, Index):
where = Index(where) if is_list else Index([where])

if self.isnull().values.all():
return pd.Series([np.nan])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work for a DataFrame (I mean: it will not be the correct return value, see the docstring)

Further, you can put this after the next line where nulls is defined and reuse that

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! thanks for the comments, feels great to learn how to contribute :)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see if you can simplify this logic a bit (maybe set the name where is_list is used before)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @jreback , I made a small simplification, pls check if that's ok... if it's ok, now I think everything is good to go

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

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/series/test_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from pandas import (offsets, Series, notnull,
isnull, date_range, Timestamp)

from pandas.util.testing import assert_series_equal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to import this, you can use it as tm.assert_series_equal(..) as is done in the other tests

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


import pandas.util.testing as tm

from .common import TestData
Expand Down Expand Up @@ -148,3 +150,8 @@ def test_errors(self):
s = Series(np.random.randn(N), index=rng)
with self.assertRaises(ValueError):
s.asof(s.index[0], subset='foo')

# series is all nans
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the issue number as a comment

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

result = Series([np.nan]).asof([0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this a separate test? (as it is not related to errors). Eg test_all_nans

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a case not using zero as the argument?
And can you also add the case of a scalar, and of multiple values? (eg s.asof(10) and s.asof([10, 11])

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

expected = Series([np.nan])
assert_series_equal(result, expected)