Skip to content

Commit 971d70e

Browse files
chris-b1AnkurDedania
authored andcommitted
BUG/API: Return a Series from DataFrame.asof with no match (pandas-dev#15232)
* BUG/API: Return a Series from DataFrame.asof no match * doc fixups
1 parent 5e233a2 commit 971d70e

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ Other API Changes
327327
- ``pd.read_csv()`` will now raise a ``ValueError`` for the C engine if the quote character is larger than than one byte (:issue:`11592`)
328328
- ``inplace`` arguments now require a boolean value, else a ``ValueError`` is thrown (:issue:`14189`)
329329
- ``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``
330-
330+
- ``DataFrame.asof()`` will return a null filled ``Series`` instead the scalar ``NaN`` if a match is not found (:issue:`15118`)
331331
.. _whatsnew_0200.deprecations:
332332

333333
Deprecations

pandas/core/generic.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -3783,7 +3783,8 @@ def asof(self, where, subset=None):
37833783
37843784
.. versionadded:: 0.19.0 For DataFrame
37853785
3786-
If there is no good value, NaN is returned.
3786+
If there is no good value, NaN is returned for a Series
3787+
a Series of NaN values for a DataFrame
37873788
37883789
Parameters
37893790
----------
@@ -3839,6 +3840,9 @@ def asof(self, where, subset=None):
38393840
start = start.ordinal
38403841

38413842
if where < start:
3843+
if not is_series:
3844+
from pandas import Series
3845+
return Series(index=self.columns, name=where)
38423846
return np.nan
38433847

38443848
# It's always much faster to use a *while* loop here for

pandas/tests/frame/test_asof.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import nose
44

55
import numpy as np
6-
from pandas import DataFrame, date_range
6+
from pandas import (DataFrame, date_range, Timestamp, Series,
7+
to_datetime)
78

8-
from pandas.util.testing import assert_frame_equal
9+
from pandas.util.testing import assert_frame_equal, assert_series_equal
910
import pandas.util.testing as tm
1011

1112
from .common import TestData
@@ -67,6 +68,23 @@ def test_subset(self):
6768

6869
assert_frame_equal(result, expected)
6970

71+
def test_missing(self):
72+
# GH 15118
73+
# no match found - `where` value before earliest date in index
74+
N = 10
75+
rng = date_range('1/1/1990', periods=N, freq='53s')
76+
df = DataFrame({'A': np.arange(N), 'B': np.arange(N)},
77+
index=rng)
78+
result = df.asof('1989-12-31')
79+
80+
expected = Series(index=['A', 'B'], name=Timestamp('1989-12-31'))
81+
assert_series_equal(result, expected)
82+
83+
result = df.asof(to_datetime(['1989-12-31']))
84+
expected = DataFrame(index=to_datetime(['1989-12-31']),
85+
columns=['A', 'B'], dtype='float64')
86+
assert_frame_equal(result, expected)
87+
7088
if __name__ == '__main__':
7189
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
7290
exit=False)

0 commit comments

Comments
 (0)