From 7b2aaa2dcd91b7c345e7278b7998807edcfc277a Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 25 Jan 2017 18:08:44 -0600 Subject: [PATCH 1/2] BUG/API: Return a Series from DataFrame.asof no match --- doc/source/whatsnew/v0.20.0.txt | 2 +- pandas/core/generic.py | 6 +++++- pandas/tests/frame/test_asof.py | 21 +++++++++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 86fa919fec304..89f0f007f811c 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..cd758d3ed1d49 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,22 @@ def test_subset(self): assert_frame_equal(result, expected) + def test_missing(self): + # GH 15118 + 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) From 5c1befe93ca6d49ef5fb97c199f57a4cc4d791ee Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 25 Jan 2017 18:39:23 -0600 Subject: [PATCH 2/2] doc fixups --- doc/source/whatsnew/v0.20.0.txt | 2 +- pandas/tests/frame/test_asof.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 89f0f007f811c..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`) + - ``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/tests/frame/test_asof.py b/pandas/tests/frame/test_asof.py index cd758d3ed1d49..f68219120b48e 100644 --- a/pandas/tests/frame/test_asof.py +++ b/pandas/tests/frame/test_asof.py @@ -70,6 +70,7 @@ def test_subset(self): 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)},