Skip to content

BUG/API: Return a Series from DataFrame.asof with no match #15232

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

Merged
merged 2 commits into from
Jan 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ Other API Changes
- ``pd.read_csv()`` will now raise a ``ValueError`` for the C engine if the quote character is larger than than one byte (:issue:`11592`)
- ``inplace`` arguments now require a boolean value, else a ``ValueError`` is thrown (:issue:`14189`)
- ``pandas.api.types.is_datetime64_ns_dtype`` will now report ``True`` on a tz-aware dtype, similar to ``pandas.api.types.is_datetime64_any_dtype``

- ``DataFrame.asof()`` will return a null filled ``Series`` instead the scalar ``NaN`` if a match is not found (:issue:`15118`)
.. _whatsnew_0200.deprecations:

Deprecations
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3783,7 +3783,8 @@ def asof(self, where, subset=None):

.. versionadded:: 0.19.0 For DataFrame

If there is no good value, NaN is returned.
If there is no good value, NaN is returned for a Series
a Series of NaN values for a DataFrame

Parameters
----------
Expand Down Expand Up @@ -3839,6 +3840,9 @@ def asof(self, where, subset=None):
start = start.ordinal

if where < start:
if not is_series:
from pandas import Series
return Series(index=self.columns, name=where)
return np.nan

# It's always much faster to use a *while* loop here for
Expand Down
22 changes: 20 additions & 2 deletions pandas/tests/frame/test_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import nose

import numpy as np
from pandas import DataFrame, date_range
from pandas import (DataFrame, date_range, Timestamp, Series,
to_datetime)

from pandas.util.testing import assert_frame_equal
from pandas.util.testing import assert_frame_equal, assert_series_equal
import pandas.util.testing as tm

from .common import TestData
Expand Down Expand Up @@ -67,6 +68,23 @@ def test_subset(self):

assert_frame_equal(result, expected)

def test_missing(self):
# GH 15118
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 a 1-liner describing this test (what its doing)

# no match found - `where` value before earliest date in index
N = 10
rng = date_range('1/1/1990', periods=N, freq='53s')
df = DataFrame({'A': np.arange(N), 'B': np.arange(N)},
index=rng)
result = df.asof('1989-12-31')

expected = Series(index=['A', 'B'], name=Timestamp('1989-12-31'))
assert_series_equal(result, expected)

result = df.asof(to_datetime(['1989-12-31']))
expected = DataFrame(index=to_datetime(['1989-12-31']),
columns=['A', 'B'], dtype='float64')
assert_frame_equal(result, expected)

if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
exit=False)