diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 86fa919fec304..07c90b1e64f29 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -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 diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 2bb492055bcd0..869062bd231fe 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -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 ---------- @@ -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 diff --git a/pandas/tests/frame/test_asof.py b/pandas/tests/frame/test_asof.py index 47d56c39757db..f68219120b48e 100644 --- a/pandas/tests/frame/test_asof.py +++ b/pandas/tests/frame/test_asof.py @@ -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 @@ -67,6 +68,23 @@ def test_subset(self): assert_frame_equal(result, expected) + def test_missing(self): + # GH 15118 + # 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)